import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class TableColumnTest extends JFrame { public TableColumnTest() { super("Simple JTable Test"); //Create the table and give the data as a two-dimensional //String array and the column headers as a one-dimensional String array. JTable jt = new JTable(new String[][] { {"This", "is", "cell 13"}, {"a", "Test", "cell 23" }, {"cell 31", "cell 32", "cell 33" } }, new String[] {"Column 1", "Column 2", "Column 3"}); //Get the columns. TableColumn c1 = jt.getColumn("Column 1"); TableColumn c2 = jt.getColumn("Column 2"); TableColumn c3 = jt.getColumn("Column 3"); //Column one has a max width of 100 and a min width of 50. c1.setMaxWidth(100); c1.setMinWidth(50); //Column two is not directly resizable. It can still //be resized as the result of another column being resized. c2.setResizable(false); //Column three has the header "Pernilla Wiberg". c3.setHeaderValue("Pernilla Wiberg"); //Place the table in a scrollpane. This is the simplest //way to make it scollable. JScrollPane jsp = new JScrollPane(jt); getContentPane().add(jsp, BorderLayout.CENTER); //Show the frame. setSize(300, 200); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } public static void main(String args[]) { new TableColumnTest(); } }