Desktop Java 8 – part 2- simple JTable component in JFrame

The simpliest table with data and column names is very easy to implement as desktop apllication.

In this post begin with create simple desktop window ( JFrame component ). Example will be  describe in last post Desktop Java 8 – part 1- creating simple window application. For create table You need data an names of columns for them. You may store names of columns in String array. But data may be locate in two-dimentional array.  The data in columns have diffrent datatype so the array must store Object data. In code below You see them.

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);
         f.add(table);
        
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
      }
        }); 
   }
}

If You have data and names of columns, create JTable object. Constuctor this class may get two parameters. First is array of data which will be store in table and second is array of names of columns. And You must add this component to the frame.
Result of call this application display table without names of columns.
qq1The table may be display names of columns and panes to scroll in vertical when You add table to JScrollPane component and JScrollPane to JFrame.

...
   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);
         JScrollPane scroll = new JScrollPane(table);
         f.add(scroll);
...

It is effect:
qq2If You make the window smaller the only vertical scroll appear.

qq3What to do to show horizontall scroll pane?
The resolve this problem is call for table object setAutoResizeMode method with JTable.AUTO_RESIZE_OFF parameter. In this situation the resize not be automatic fit to window.

...   
         JTable table = new JTable(data, columns);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         JScrollPane scroll = new JScrollPane(table);
         f.add(scroll);
...

It is result after call application:
qq4
An it is result after make smaller window:
qq5This table was created with default table model( DefaultTableModel class). You may prepare own table model in JTable component.