Hai Friends.. Good Day....
Your program is good that has no problem. But you can use TableColumn and DefaultTableColumnModel instead of directly add column to table. It has more methods to change column's model, width, fontcolor etc.,
Here is Sample Program...
import javax.swing.*;
import javax.swing.table.*;
/**
TableColumn and DefaultTableColumnModel both are present in
javax.swing.tree package
*/
public class DemoTable extends JFrame {
JTable table;
TableColumn column1, column2, column3;
DefaultTableColumnModel headers;
DefaultTableModel rows;
public DemoTable() {
setTitle("Table");
//create and define columns
column1 = new TableColumn(0);
column1.setHeaderValue("First Name"); // set column name
column1.setPreferredWidth(50); //set column width
column2 = new TableColumn(1);
column2.setHeaderValue("Last Name");
column2.setPreferredWidth(100);
column3 = new TableColumn(2);
column3.setHeaderValue("Year");
column3.setPreferredWidth(120);
// A DefaultTableColumnModel object is created and the three
// TableColumn objects are added to it.
headers = new DefaultTableColumnModel();
headers.addColumn(column1);
headers.addColumn(column2);
headers.addColumn(column3);
//Create two vector objects and load them with the data
//to be placed on each row of the table
Object[] v1 = {"Lisa","Reid",new Integer(1990) };
Object[] v2 = {"Cherry","Spada",new Integer(1989) };
// A DefaultTableModel object is created initally with 0 rows and 3 columns.
// two rows are added containing the data int the object array.
rows = new DefaultTableModel(0,3);
rows.addRow(v1);
rows.addRow(v2);
table = new JTable(rows, headers);
getContentPane().add(new JScrollPane(table));
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(
String args[]) {
new DemoTable();
}
}
I hope this is help you..
Bye...