The JTable constructor accepts two arguments 2dimension Object Array for the data, and String Array for the column names.
eg:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Test6 extends JFrame {
public Test6(){
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Mpanel m = new Mpanel();
this.add(m,BorderLayout.CENTER);
}
class Mpanel extends JPanel {
JTable mTable;
private Object[][] cells = {{"Vivek",10.00},{"Vishal",20.00}};
private String[] columnNames = { "Planet", "Radius" };
JScrollPane mScroll;
public Mpanel(){
this.setSize(150,150);
this.setComponent();
}
public void setComponent(){
mTable = new JTable(cells,columnNames);
mTable.setAutoCreateRowSorter(true);
mScroll = new JScrollPane(mTable);
this.add(mScroll);
}
}
public static void main(String[] args){
new Test6().setVisible(true);
}
}