Nikhil
Nikhil

Reputation: 2883

How can i display the column name of a table

I use the following code to display a table.

final Vector<Vector<String>> vct = refreshDatas();
final Vector<String> Cols = new Vector<String>();
Cols.add("OID");
Cols.add("Name");
this.tmodel = new DefaultTableModel(vct,Cols);
this.table.setModel(this.tmodel);
this.table.setBounds(50, 200, 300, 250);
this.table.setSize(200, 200);
this.table.setVisible(true);

but only the contents is displayed. The header OID and Name are not displayed.

Upvotes: 3

Views: 96

Answers (2)

Nikhil
Nikhil

Reputation: 2883

just add the table in to the JScrollPane, It automatically display the table headers.

Upvotes: 2

Robin
Robin

Reputation: 36621

See the 'Adding a table to a container' section in the table tutorial. If you add the table yourself, you must make the headers visible as well. If you add your table to a scrollpane, the scrollpane will take care of this for you.

Copy-paste from that tutorial:

If you are using a table without a scroll pane, then you must get the table header component and place it yourself. For example:

container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);

Sidenote: it should not be needed to call setBounds nor setSize. Just make sure your parent Container has a decent LayoutManager and it will take care of the size

Upvotes: 2

Related Questions