JTable:

JTable is a component that displays rows and columns of data. We can drag the cursor on column boundaries to resize columns. JTable has many classes and interfaces associated with it. These are packaged in javax.swing.table. JTable supplies several constructors, one of these are as follows:
JTable(Object data[][], Object colHeads[])
Here, data is a two-dimensional array of the information to be presented, and colHeads is a one-dimensional array with the column headings. JTable relies on three models: The first is the table model, which is defined by the TableModel interface. This model defines those things related to displaying data in a two-dimensional format.
The second is the table column model, which is represented by TableColumnModel. TableColumnModel specifies the characteristics of a column. These two models are packaged in javax.swing.table.
The third model determines how items are selected, and it is specified by ListSelectionModel.
A JTable generates several different events. The two most fundamental to a table’s operation are ListSelectionEvent and TableModelEvent. A ListSelectionEvent is generated when the user selects something in the table. A TableModelEvent is fired when that table’s data changes in some way. 
Simple steps that are used to setup a JTable is as follows:
1.      Create an instance of JTable.
2.      Create a JScrollPane object, specifying the table as the object to scroll.
3.      Add the table to the scroll pane.
4.      Add the scroll pane to the content pane.
Example:
import java.awt.*;
import javax.swing.*;
public class MyTable extends JApplet{
  public void init() {
   try
   {
   SwingUtilities.invokeAndWait(new Runnable()
   {
      public void run()
      {
          makeGUI();
      }
    });
  } catch(Exception e){
           System.out.println("Sorry some error occured "+e);
        }
 }
 private void makeGUI() {
   String colHead[]={"Name","ID","Salary"};
   Object data[][]={
   {"Santosh","1001","12000"},
   {"Rahul","1002","12500"},
   {"Santu","1003","15000"},
   {"Sakit","1004","16800"},
   {"Tuki","1005","12500"},
   {"Subh","1006","11000"},
   {"Suman","1007","17000"},
   {"Soni","1008","16000"}
  };
   JTable t=new JTable(data,colHead);
   JScrollPane scr=new JScrollPane(t);
   add(scr);
 }
}
Output:

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.