Java JTable create from two dimensional object array

Description

Java JTable create from two dimensional object array

// Demonstrate JTable. 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

class Demo extends JPanel { 
   //  ww w.  j  av a2s. c  o  m
   public Demo() { 

     // Initialize column headings. 
     String[] colHeads = { "Name", "Extension", "ID#" }; 
  
     // Initialize data. 
     Object[][] data = { 
       { "CSS", "4", "8" }, 
       { "HTML", "7", "5" }, 
       { "Java", "1", "5" }, 
       { "Javascript", "5", "112" }, 
       { "SQL", "9", "133" }, 
       { "C++", "6", "145" } 
     }; 
  
     // Create the table. 
     JTable table = new JTable(data, colHeads); 
  
     // Add the table to a scroll pane. 
     JScrollPane jsp = new JScrollPane(table); 
  
     // Add the scroll pane to the content pane. 
     add(jsp); 
   } 
 }

public class Main {
   public static void main(String[] args) {
      Demo panel = new Demo();

      JFrame application = new JFrame();

      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      application.add(panel);
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related