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

import
javax.swing.*;// for uesing the swing coponet like JButton,JTextField JScrollPane etc
import java.awt.event.*;//for performing event handling
import
javax.swing.table.*;// for creating a DefaultTableModel

Step 2: Define the data members of componets to be used

       JFrame fr1,fr2;
       JScrollPane
sb;
       JTextField
tfName,tfRollNo,tfMark;
       JButton
bAdd,bShow;
       JTable
tb;
       DefaultTableModel
model;
       JLabel jl1,jl2,jl3,jl4;

Step 3: Create a frames object and set the layout to null.

fr1=new JFrame();
fr2=new JFrame();
fr1.setLayout(null);
fr2.setLayout(null);

Step 4: Create the Labels, Buttons and TextFields objects and call setBounds for them to set the position in
the frame.

jl1=new JLabel("Ente Name");
jl2=new JLabel("Ente Roll No");
jl3=new JLabel("Ente Mark");
jl4=new JLabel("Your Data in Table Format");
jl1.setBounds(30,50,100,40);
jl2.setBounds(30,120,100,40);
jl3.setBounds(30,190,100,40);
jl4.setBounds(60,30,200,20);
tfName=new JTextField();
tfRollNo=new JTextField();
tfMark=new JTextField();
tfName.setBounds(250,50,100,40);
tfRollNo.setBounds(250,120,100,40);
tfMark.setBounds(250,190,100,40);
bAdd=new JButton("ADD");
bShow=new JButton("Show");
bAdd.setBounds(10,270,100,50);
bShow.setBounds(180,270,100,50);
tb=new JTable();
sb=new JScrollPane(tb);
sb.setBounds(30,60,150,100);

step 5: Add the component to the appropriate frame.

fr1.add(tfName);

fr1.add(tfRollNo);
fr1.add(tfMark);

fr1.add(bAdd);

fr1.add(bShow);

fr1.add(jl1);

fr1.add(jl2);

fr1.add(jl3);

fr2.add(jl4);

fr2.add(sb);

Step 6: Attach the button to an appropriate Listener (that is called registration to source) and set the frame size.

bAdd.addActionListener(this);
bShow.addActionListener(this);
fr1.setSize(400,400);
fr2.setSize(300,300);
fr1.setVisible(true);

Step 7: Create the object DefaultTableModel and add the column with their name. And set the table model.
 

model=new DefaultTableModel();
model.addColumn("Name");

model.addColumn("RollNo");

model.addColumn("Mark");
tb.setModel(model);


Step 8: Write the code for the actionPerformed Method.
 

public void actionPerformed(ActionEvent ae)

{
if(ae.getSource()==bAdd)

{

String r[]=new String[5];

r[0]=tfName.getText();

r[1]=tfRollNo.getText();

r[2]=tfMark.getText();

tfName.setText("");

tfRollNo.setText("");

tfMark.setText("");

model.addRow(r);

}

if (ae.getSource()==bShow)

{
fr2.setVisible(true);

}

}

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

import
javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
class JTableTest implements ActionListener
{
JFrame
fr1,fr2;
JScrollPane
sb;
JTextField tfName,tfRollNo,tfMark;
JButton
bAdd,bShow;
JTable
tb;
DefaultTableModel
model;
JLabel
jl1,jl2,jl3,jl4;
JTableTest()
{
fr1=new JFrame();
fr2=new JFrame();
fr1.setLayout(null);
fr2.setLayout(null);
jl1=new JLabel("Ente Name");
jl2=new JLabel("Ente Roll No");
jl3=new JLabel("Ente Mark");
jl4=new JLabel("Your Data in Table Format");
jl1.setBounds(30,50,100,40);
jl2.setBounds(30,120,100,40);
jl3.setBounds(30,190,100,40);
jl4.setBounds(60,30,200,20);
tfName=new JTextField();
tfRollNo=new JTextField();
tfMark=new JTextField();
tfName.setBounds(250,50,100,40);
tfRollNo.setBounds(250,120,100,40);
tfMark.setBounds(250,190,100,40);
bAdd=new JButton("ADD");
bShow=new JButton("Show");
bAdd.setBounds(10,270,100,50);
bShow.setBounds(180,270,100,50);
tb=new JTable();
sb=new JScrollPane(tb);
sb.setBounds(30,60,150,100);
fr1.add(tfName);
fr1.add(tfRollNo);
fr1.add(tfMark);
fr1.add(bAdd);
fr1.add(bShow);
fr1.add(jl1);
fr1.add(jl2);
fr1.add(jl3);
fr2.add(jl4);
fr2.add(sb);
bAdd.addActionListener(this);
bShow.addActionListener(this);
fr1.setSize(400,400);
fr2.setSize(300,300);
fr1.setVisible(true);
model=new DefaultTableModel();
model.addColumn("Name"):
model.addColumn("RollNo");
model.addColumn("Mark");
tb.setModel(model);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==bAdd)
{
String r[]=
new String[5];
r[0]=
tfName.getText();
r[1]=tfRollNo.getText();
r[2]=tfMark.getText();
tfName.setText("");
tfRollNo.setText("");
tfMark.setText("");
model.addRow(r);
}
if (ae.getSource()==bShow)
{
fr2.setVisible(true);
}
}
public static void main(String[] args)
{
new JTableTest();
}
}

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

Using statement in C#
Namespaces in C#
Creating C# Console Application using a Text Editor
Selecting and Modifying elements using JQuery

Up Next
    Ebook Download
    View all
    Learn
    View all