views:

253

answers:

4

I would like to create a table with the constructor method JTable(TableModel). What exact methods in the TableModel do I need to display the titles of each column?

+1  A: 

You need to embed your JTable within a JScrollPane and the column headings will be shown automatically:

JScrollPane sp = new JScrollPane(new JTable());
Adamski
Thanks Adamski. There is however a small problem: I am using the GridBagLayout. when I add the JScrollPane with the JTable embedded to the container with this layout, the JTable is reduced to a very small size. I still did not manage to understand why....
Tony
Have you added the scrollpane with the appropriate fill, weightx and weighty values?
willcodejavaforfood
the values you need are fill=GridBagConstraints.BOTH and weigthx and weighty values need to be relatively big in relation to the sum of theese values in their respective row/column.Ideally weigthx and weighty for the jscrollpane will be 1 (in base 1) and 0 in the rest of the components in the same row/column
Telcontar
Many thanks for the help
Tony
A: 

You need to implement a TableModel for example by extending the class AbstractTableModel or by to using a DefaultTableModel. This later has a constructor where you can set the number and names of your columns.

Pierre
A: 
sateesh
A: 

I think what you are really looking for the is the class DefaultTableModel. Just read through the documentation and you will be on your own way.

ultrajohn