The Use of JTable in Swing

This article describes Java Swing and makes a frame containing three labels, three text fields and two buttons. When you enter the data and click on the add button, then click on the show button then a new frame is opened and the frame contains a table. And the table has three fields named Name, Roll No and Marks for better understanding.
 
Step 1: Import necessary packages
  1. import javax.swing.*;// for uesing the swing coponet like JButton,JTextField JScrollPane etc  
  2. import java.awt.event.*;//for performing event handling   
  3. import javax.swing.table.*;// for creating a DefaultTableModel   
Step 2: Define the data members of components to be used
  1. JFrame fr1,fr2;  
  2. JScrollPane sb;  
  3. JTextField tfName,tfRollNo,tfMark;  
  4. JButton bAdd,bShow;  
  5. JTable tb;  
  6. DefaultTableModel model;  
  7. JLabel jl1,jl2,jl3,jl4;  
Step 3: Create a frames object and set the layout to null.
  1. fr1=new JFrame();  
  2. fr2=new JFrame();  
  3. fr1.setLayout(null);  
  4. fr2.setLayout(null);  
Step 4: Create the Labels, Buttons and TextFields objects and call setBounds for them to set the position in the frame.
  1. jl1=new JLabel("Ente Name");  
  2. jl2=new JLabel("Ente Roll No");  
  3. jl3=new JLabel("Ente Mark");  
  4. jl4=new JLabel("Your Data in Table Format");  
  5. jl1.setBounds(30,50,100,40);  
  6. jl2.setBounds(30,120,100,40);  
  7. jl3.setBounds(30,190,100,40);  
  8. jl4.setBounds(60,30,200,20);  
  9. tfName=new JTextField();  
  10. tfRollNo=new JTextField();  
  11. tfMark=new JTextField();  
  12. tfName.setBounds(250,50,100,40);  
  13. tfRollNo.setBounds(250,120,100,40);  
  14. tfMark.setBounds(250,190,100,40);  
  15. bAdd=new JButton("ADD");  
  16. bShow=new JButton("Show");  
  17. bAdd.setBounds(10,270,100,50);  
  18. bShow.setBounds(180,270,100,50);  
  19. tb=new JTable();  
  20. sb=new JScrollPane(tb);  
  21. sb.setBounds(30,60,150,100);  
Step 5: Add the component to the appropriate frame.
  1. fr1.add(tfName);  
  2. fr1.add(tfRollNo);  
  3. fr1.add(tfMark);  
  4. fr1.add(bAdd);  
  5. fr1.add(bShow);  
  6. fr1.add(jl1);  
  7. fr1.add(jl2);  
  8. fr1.add(jl3);  
  9. fr2.add(jl4);  
  10. fr2.add(sb);  
Step 6: Attach the button to an appropriate Listener (that is called registration to source) and set the frame size.
  1. bAdd.addActionListener(this);  
  2. bShow.addActionListener(this);  
  3. fr1.setSize(400,400);  
  4. fr2.setSize(300,300);  
  5. fr1.setVisible(true);  
Step 7: Create the object DefaultTableModel and add the column with their name. And set the table model. 
  1. model=new DefaultTableModel();  
  2. model.addColumn("Name");  
  3. model.addColumn("RollNo");  
  4. model.addColumn("Mark");  
  5. tb.setModel(model);  
Step 8: Write the code for the actionPerformed Method. 
  1. public void actionPerformed(ActionEvent ae)  
  2. {  
  3.     if (ae.getSource() == bAdd)  
  4.     {  
  5.         String r[] = new String[5];  
  6.         r[0] = tfName.getText();  
  7.         r[1] = tfRollNo.getText();  
  8.         r[2] = tfMark.getText();  
  9.         tfName.setText("");  
  10.         tfRollNo.setText("");  
  11.         tfMark.setText("");  
  12.         model.addRow(r);  
  13.     }  
  14.     if (ae.getSource() == bShow)  
  15.     {  
  16.         fr2.setVisible(true);  
  17.     }  
  18. }  
Step 9: Rest only the Action performed method all the things write with in a constructor so we need only create the object of my class with the main method.
 
Complete code
  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.table.*;  
  4. class JTableTest implements ActionListener {  
  5.     JFrame fr1, fr2;  
  6.     JScrollPane sb;  
  7.     JTextField tfName, tfRollNo, tfMark;  
  8.     JButton bAdd, bShow;  
  9.     JTable tb;  
  10.     DefaultTableModel model;  
  11.     JLabel jl1, jl2, jl3, jl4;  
  12.     JTableTest() {  
  13.         fr1 = new JFrame();  
  14.         fr2 = new JFrame();  
  15.         fr1.setLayout(null);  
  16.         fr2.setLayout(null);  
  17.         jl1 = new JLabel("Ente Name");  
  18.         jl2 = new JLabel("Ente Roll No");  
  19.         jl3 = new JLabel("Ente Mark");  
  20.         jl4 = new JLabel("Your Data in Table Format");  
  21.         jl1.setBounds(305010040);  
  22.         jl2.setBounds(3012010040);  
  23.         jl3.setBounds(3019010040);  
  24.         jl4.setBounds(603020020);  
  25.         tfName = new JTextField();  
  26.         tfRollNo = new JTextField();  
  27.         tfMark = new JTextField();  
  28.         tfName.setBounds(2505010040);  
  29.         tfRollNo.setBounds(25012010040);  
  30.         tfMark.setBounds(25019010040);  
  31.         bAdd = new JButton("ADD");  
  32.         bShow = new JButton("Show");  
  33.         bAdd.setBounds(1027010050);  
  34.         bShow.setBounds(18027010050);  
  35.         tb = new JTable();  
  36.         sb = new JScrollPane(tb);  
  37.         sb.setBounds(3060150100);  
  38.         fr1.add(tfName);  
  39.         fr1.add(tfRollNo);  
  40.         fr1.add(tfMark);  
  41.         fr1.add(bAdd);  
  42.         fr1.add(bShow);  
  43.         fr1.add(jl1);  
  44.         fr1.add(jl2);  
  45.         fr1.add(jl3);  
  46.         fr2.add(jl4);  
  47.         fr2.add(sb);  
  48.         bAdd.addActionListener(this);  
  49.         bShow.addActionListener(this);  
  50.         fr1.setSize(400400);  
  51.         fr2.setSize(300300);  
  52.         fr1.setVisible(true);  
  53.         model = new DefaultTableModel();  
  54.         model.addColumn("Name"):  
  55.             model.addColumn("RollNo");  
  56.         model.addColumn("Mark");  
  57.         tb.setModel(model);  
  58.     }  
  59.     public void actionPerformed(ActionEvent ae) {  
  60.         if (ae.getSource() == bAdd) {  
  61.             String r[] = new String[5];  
  62.             r[0] = tfName.getText();  
  63.             r[1] = tfRollNo.getText();  
  64.             r[2] = tfMark.getText();  
  65.             tfName.setText("");  
  66.             tfRollNo.setText("");  
  67.             tfMark.setText("");  
  68.             model.addRow(r);  
  69.         }  
  70.         if (ae.getSource() == bShow) {  
  71.             fr2.setVisible(true);  
  72.         }  
  73.     }  
  74.     public static void main(String[] args) {  
  75.         new JTableTest();  
  76.     }  
  77. }  
Output
 
img5.jpg 
 
The blank form is opened at first.
 
img1.jpg 
 
This is the second form containing the table and it opens when the show button is clicked.
 
img2.jpg 
 
Now put the value in each text box and click the add button.
 
img3.jpg 
 
Now click on the show button
 
img4.jpg 
 
Resourses


Similar Articles