The program uses JTable class which is used to display data in tabular form. 2 array objects to store the data and the title of the table & scrolPane for providing a scrollable view.
Classes and Methods Used in program
- JFrame Class: JFrame s is a window with borders and title. It places several components including container object
- JTable: This class is used to display & edit the data stored in tabular form.There are various constructors used to display Table but the constructor used in program is as follows:
JTable (Object [][]data,String [] title)
It constructs a table to display the data in two dimentional array, data with column name as title
JScrollPane Class: This class provides a scrollable area. It manages a viewport with horizontal & vertical scrollbars
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableDemo extends JFrame
{
JTable jtable;
JScrollPane jsp;
Container cp;
Object [][]data = {{“Amit”,”Goyenka”,”System Administrator”,new Integer(5)},{“Jitesh”,”Kabra”,”System Analyst”,new Integer(4)},{“Kunal”,”Shah”,”Web Designer”,new Integer(3)},{“Rahul”,”Mishra”,”Peon”,new Integer(2)},{“Paresh”,”Patel”,”Software Testor”,new Integer(4)}};
String [] title = {“First Nmae”,”Last Name”,”Desgnation”,”Experience”};
TableDemo()
{
super();
cp = getContentPane();
cp.setLayout(new FlowLayout());
jtable = new JTable(data,title);
jsp = new JScrollPane(jtable);
cp.add(jsp);
}
public static void main(String[] args)
{
// TODO code application logic here
TableDemo t1 = new TableDemo();
t1.setVisible(true);
t1.setSize(500,300);
t1.setResizable(false);
t1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}