SEARCH HERE

Tuesday, December 27, 2022

Java JTable

 

Java JTable

The JTable class is used to display data in tabular form. It is composed of rows and columns.

JTable class declaration

Let's see the declaration for javax.swing.JTable class.

Commonly used Constructors:

ConstructorDescription
JTable()Creates a table with empty cells.
JTable(Object[][] rows, Object[] columns)Creates a table with the specified data.

Java JTable Example

  1. import javax.swing.*;    
  2. public class TableExample {    
  3.     JFrame f;    
  4.     TableExample(){    
  5.     f=new JFrame();    
  6.     String data[][]={ {"101","Amit","670000"},    
  7.                           {"102","Jai","780000"},    
  8.                           {"101","Sachin","700000"}};    
  9.     String column[]={"ID","NAME","SALARY"};         
  10.     JTable jt=new JTable(data,column);    
  11.     jt.setBounds(30,40,200,300);          
  12.     JScrollPane sp=new JScrollPane(jt);    
  13.     f.add(sp);          
  14.     f.setSize(300,400);    
  15.     f.setVisible(true);    
  16. }     
  17. public static void main(String[] args) {    
  18.     new TableExample();    
  19. }    
  20. }  

Output:

JAVA Jtable 1

Java JTable Example with ListSelectionListener

  1. import javax.swing.*;    
  2. import javax.swing.event.*;  
  3. public class TableExample {    
  4.       public static void main(String[] a) {  
  5.             JFrame f = new JFrame("Table Example");  
  6.             String data[][]={ {"101","Amit","670000"},    
  7.                                                                        {"102","Jai","780000"},    
  8.                                                                        {"101","Sachin","700000"}};    
  9.                             String column[]={"ID","NAME","SALARY"};         
  10.                             final JTable jt=new JTable(data,column);    
  11.             jt.setCellSelectionEnabled(true);  
  12.             ListSelectionModel select= jt.getSelectionModel();  
  13.             select.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
  14.             select.addListSelectionListener(new ListSelectionListener() {  
  15.               public void valueChanged(ListSelectionEvent e) {  
  16.                 String Data = null;  
  17.                 int[] row = jt.getSelectedRows();  
  18.                 int[] columns = jt.getSelectedColumns();  
  19.                 for (int i = 0; i < row.length; i++) {  
  20.                   for (int j = 0; j < columns.length; j++) {  
  21.                     Data = (String) jt.getValueAt(row[i], columns[j]);  
  22.                   } }  
  23.                 System.out.println("Table element selected is: " + Data);    
  24.               }       
  25.             });  
  26.             JScrollPane sp=new JScrollPane(jt);    
  27.             f.add(sp);  
  28.             f.setSize(300200);  
  29.             f.setVisible(true);  
  30.           }  
  31.         }  

Output:

JAVA Jtable 2

If you select an element in column NAME, name of the element will be displayed on the console:

  1. Table element selected is: Sachin  

0 comments:

Post a Comment

C++

AJAVA

C

E-RESOURCES

LKG, UKG Live Worksheets

Top