Desktop Java 8 – part 3- how fit table and sort its data in JFrame?

How fit table  in frame in desktop Java application and sort data in it? I describe in this post step by step how do it.

In last topic about desktop Java application I created simple table: Desktop Java 8 – part 2- simple JTable component in JFrame. In this post I extend it to fitting view of table to the frame. Beginn with this base example:

package doraprojects;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class SimpleTable 
{
    
public static final int WIDTH_WINDOW = 400;
public static final int HEIGHT_WINDOW = 200;

    public static void main(String[] args) 
    {
        
    EventQueue.invokeLater(new Runnable(){
     public void run()
     {    
        JFrame f = new JFrame("My window");
        f.setSize(WIDTH_WINDOW, HEIGHT_WINDOW); 
        f.getContentPane().setBackground(Color.white);
        
           String[] columns = { "ID","Name","product","price in $" };  
           Object[][] data = { {1234,"John","pencil",10},
                               {4382,"Mirian","pen",11}, 
                               {2345,"Katy","chair",253}, 
                               {6334,"Agnes","table",456}, 
                               {1177,"Stanley","flower",25} }; 
            
         JTable table = new JTable(data, columns);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         JScrollPane scroll = new JScrollPane(table);
         f.add(scroll);
        
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
      }
        }); 
   }
}

If You call it You see:
qq4Sometimes we need fit table component to size of desktop window. To achive this aim help setFillsVieportHeight method. This function get only one parameter. The false is default value. The true allow table fill up all height of frame.

...
 String[] columns = { "ID","Name","product","price in $" };  
           Object[][] data = { {1234,"John","pencil",10},
                               {4382,"Mirian","pen",11}, 
                               {2345,"Katy","chair",253}, 
                               {6334,"Agnes","table",456}, 
                               {1177,"Stanley","flower",25} }; 
            
         JTable table = new JTable(data, columns);
         table.setFillsViewportHeight(true);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         JScrollPane scroll = new JScrollPane(table);
         f.add(scroll);
...

The result:

You may either determine the width of columns, You to do it through the getColumnModel method. It return TableColumnModel object. For this object calling getColumn function with index of column as parameter, You get TableColumn object. This object has setPreferredWidth method setting width in pixels of concrete column.

...
 String[] columns = { "ID","Name","product","price in $" };  
           Object[][] data = { {1234,"John","pencil",10},
                               {4382,"Mirian","pen",11}, 
                               {2345,"Katy","chair",253}, 
                               {6334,"Agnes","table",456}, 
                               {1177,"Stanley","flower",25} }; 
            
         JTable table = new JTable(data, columns);
         table.setFillsViewportHeight(true);
         table.getColumnModel().getColumn(0).setPreferredWidth(80);
         table.getColumnModel().getColumn(1).setPreferredWidth(100);
         table.getColumnModel().getColumn(2).setPreferredWidth(150);
         table.getColumnModel().getColumn(3).setPreferredWidth(100);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         JScrollPane scroll = new JScrollPane(table);
         f.add(scroll);
...

Result:
In this table You don’t sort data in columns. The setAutoCreateSortRowSorter method allow for it. It get one parameter as boolean value. If You set true value, clicking on the header concrete column, show rows according to sorted data in column ascending, another click on the header of the same column sort rows according to sorted data in this column descending.

...
 String[] columns = { "ID","Name","product","price in $" };  
           Object[][] data = { {1234,"John","pencil",10},
                               {4382,"Mirian","pen",11}, 
                               {2345,"Katy","chair",253}, 
                               {6334,"Agnes","table",456}, 
                               {1177,"Stanley","flower",25} }; 
            
         JTable table = new JTable(data, columns);
         table.setFillsViewportHeight(true);
         table.getColumnModel().getColumn(0).setPreferredWidth(80);
         table.getColumnModel().getColumn(1).setPreferredWidth(100);
         table.getColumnModel().getColumn(2).setPreferredWidth(150);
         table.getColumnModel().getColumn(3).setPreferredWidth(100);
         table.setAutoCreateRowSorter(true);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         JScrollPane scroll = new JScrollPane(table);
         f.add(scroll);
...

After call application:

After clicked the Name column first time:

After clicked the Name column second time:
You may either owing to call this method, drag and drop columns changing its places: