Using swing JFrame we can easily display any tabular data by using JTable.
Adding JTable
We will display one JTable by adding two rows of data with one header column. To display any column Header along with the data we must add JScrollPane and place the JTable inside it. If we don’t require the header then adding JScrollPane is not required.
My_JTable.java
Create the file by clicking icon below File or by pressing Ctrl and N .
Other .. > JFrame > JTable1>Finish
Go to Design Tab and add absolute Layout
Layout > Absolute
Adding JScrollPane
Containers > JScrollPane
Give one area to JScrollPane on your JPanel to keep the JTable inside it.
Go to Components and select JTable, Place the JTable inside the JScrollPane.
Adding data and header column.
Use the Source Tab.
We will create two String arrays to store column header and two rows of data.
public JTable1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 286);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(26, 46, 352, 131);
contentPane.add(scrollPane);
String[][] data = {{"Alex","Five","55","Male"},
{"Ron","Six","65","Femal"}};
String[] column= {"Name","Class","Mark","Sex"};
jt1 = new JTable(data,column);
scrollPane.setViewportView(jt1);
}
download
Download the Java Files here ( right click and save link as to download files ) .