A table is a combination of cells organized in an order form. In Java Swing, the JTable class gives us the ability to create a Table. Table easy to represent data with. And the table can be very easy that one can understand the information with.           

Table In Java Swing  

In java, the JTable class, which is used for the creation of the Table is the subclass of the JComponent. To set up a simple table the JTable class his default look and feel along with the default setting to make it easy for the programmer to create a simple table. but The JTable class provides us numerous methods to customize the Table. The table can be used to customize the Layout sometimes. Hah funny when I was a beginner I used to do so, but you should not do so, it is a bad practice.      

Creating Table in Java Swing  

There are a lot of ways we can create a table. we will have a look at the constructor of the JTable class, and we will understand what are the different ways we can create a Table. You can choose the one which is suitable for you. below is the list of constructors with the explanation.  

JTable() the Default Constructor

This is the default constructor of the JTable class. By using this constructor to initialize the Jtable will just create a default Table. Below is an example of using this constructor we have created a simple Table. If we can see the below code it will not display a table because there are no rows nor columns.  

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package AlixaProDev.EventListeners;
import javax.swing.JTable;
import javax.swing.JFrame;
public class TableDemo {
    JFrame f=new JFrame();
    TableDemo(){
        JTable table = new JTable();
        f.add(table);
        f.setSize(200,200);
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new TableDemo();
    }
}  

JTable(rows,cols)

This constructor lets us create a table with the specified number of rows and columns that we can pass in the parameter list. The result will be still an empty cell. We will have to put the data explicitly. Look at the below example of this constructor how we can create a table with this constructor. These cells that we are creating in this table are by default editable like we can insert data in it just like the TextField that we have studied before.   

1
2
3
4
5
6
7
8
9
    TableDemo(){
        JTable table = new JTable(4,4);
        f.add(table);
        f.setSize(200,200);
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new TableDemo();
    }

JTable(dataforRows,nameOfColumns)

With the help of this constructor, we can directly set the data in the rows and we can also set the names of the columns. The parameter that we have to pass will be of type Object. The first Argument that we have to pass will be a two-dimensional Array while the column list will be a single array of the type Object. Below is the example code that will create a table with the data in there.  

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package AlixaProDev.EventListeners;
import javax.swing.*;

public class TableDemo {
    JFrame frame=new JFrame();
    // lets create a two dimensinal array for the rows data
    // in our case we have defined them of the type of Object
    // you can define it whatever type you want like String
    // Or Integer
    Object [][] dataForRows;
    Object [] colNames ;
    TableDemo(){
        colNames = new Object[] {"FirstCol","SecondCol"};
        dataForRows = new Object[][]{{"1","2"},{"3","4"}};
        JTable table = new JTable(dataForRows   ,colNames);
        table.setBounds(50,50,200,300);
        // we need to create a scorll panne and then 
        // add our table to the scroll pane 
        // otherwise the column names will not be appearing 
        // on the Screen .
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane);
        frame.setSize(300,400);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        new TableDemo();
    }
}  

JTable(Tmodel)

This constructor allows us to create the Table with the default table model. in this case, we first have to create the object or instance of the default model and then we have to pass this to our constructor. Look at the below code, This is how this constructor works. In the below code there is a table but it is not appearing on the screen because we did not specify any rows and columns in the table.

1:  package AlixaProDev.EventListeners;  
2:  import javax.swing.*;  
3:  import javax.swing.event.TableModelListener;  
4:  import javax.swing.table.TableModel;  
5:    
6:  public class TableDemo {  
7:    JFrame frame=new JFrame();  
8:    TableDemo(){  
9:      TableModel model = new TableModel() {  
10:        @Override  
11:        public int getRowCount () {  
12:          return 0;  
13:        }  
14:    
15:        @Override  
16:        public int getColumnCount () {  
17:          return 0;  
18:        }  
19:    
20:        @Override  
21:        public String getColumnName (int columnIndex) {  
22:          return null;  
23:        }  
24:    
25:        @Override  
26:        public Class<?> getColumnClass (int columnIndex) {  
27:          return null;  
28:        }  
29:    
30:        @Override  
31:        public boolean isCellEditable (int rowIndex, int columnIndex) {  
32:          return false;  
33:        }  
34:    
35:        @Override  
36:        public Object getValueAt (int rowIndex, int columnIndex) {  
37:          return null;  
38:        }  
39:    
40:        @Override  
41:        public void setValueAt (Object aValue, int rowIndex, int columnIndex) {  
42:    
43:        }  
44:    
45:        @Override  
46:        public void addTableModelListener (TableModelListener l) {  
47:    
48:        }  
49:    
50:        @Override  
51:        public void removeTableModelListener (TableModelListener l) {  
52:    
53:        }  
54:      };  
55:      JTable table = new JTable(model);  
56:      table.setBounds(50,50,200,300);  
57:      JScrollPane scrollPane = new JScrollPane(table);  
58:      frame.add(scrollPane);  
59:      frame.setSize(300,400);  
60:      frame.setVisible(true);  
61:    }  
62:    public static void main(String[] args) {  
63:      new TableDemo();  
64:    }  
65:  }   

Methods in the JTable Class

JTable class provides us a lot of methods that can help us create a very customized Table. The complete list of the methods that are over there in the JTable class is in there oracle.com. I will mention a few of them and with the help, we will create our Table. 

Adding a column to the existing table

The method addColumn is used to add columns to the table. the method accepts one argument which is the name of the table-column. We will look at the example of this method.

Count the number of columns

To count the number of columns that are over there. we can use the method named is get column count() which returns the number of the column.

Return the name of the Column

We can use the method getColumnName for this purpose. this method is used to return the name of the column at the index of the column we have to pass the index of the column and then it returns the name of that column.

Return the count of the Rows

To get the number of rows we can use the getRowCount() method that will return us the number of rows that are there in the Table.

Return the selected Rows

The getSelectedRows() method can be used to get the number of selected rows. Below is the example program which uses all these methods. we will have a deep look at the table class when we are working with the event handling on the Table. but for now, look at the following program to better understand how to use the table.

1:  package AlixaProDev.EventListeners;  
2:  import javax.swing.*;  
3:  import java.awt.*;  
4:    
5:  public class TableDemo {  
6:    Font myFavFont;  
7:    JFrame frame=new JFrame();  
8:    TableDemo(){  
9:      myFavFont = new Font("Mv Boli",Font.ITALIC,34);  
10:      String [] [] rowsData = {{"1","Hazrat ALi"},{"2","ALixaDev"}};  
11:      String [] colName = {"RollNo","Name"};  
12:    
13:    
14:      JTable table = new JTable(rowsData,colName);  
15:      table.setBounds(50,50,500,300);  
16:      table.setBackground(Color.MAGENTA);  
17:      table.setRowHeight(45);  
18:      table.setFont(myFavFont);  
19:      JScrollPane scrollPane = new JScrollPane(table);  
20:      frame.add(scrollPane);  
21:      frame.setTitle("AlixaProDev");  
22:      frame.setSize(500,400);  
23:      frame.setVisible(true);  
24:    }  
25:    public static void main(String[] args) {  
26:      new TableDemo();  
27:    }  
28:  }   

Summary 

Hi, the above code output us a very beautiful table that you can see in the picture. But this is not what you can make with the swing JTable. You can make a very beautiful table by reading the article. This is the picture that is the output of the above program.

Table in java

Post a Comment

Previous Post Next Post