Display Table Titles with a Table Model in Java Swing?

You need to embed your JTable within a JScrollPane and the column headings will be shown automatically: JScrollPane sp = new JScrollPane(new JTable()).

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.... – Anto Jan 22 '10 at 11:12 Have you added the scrollpane with the appropriate fill, weightx and weighty values? – willcodejavaforfood Jan 22 '10 at 11:23 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 Jan 22 '10 at 11:47 Many thanks for the help – Anto Jan 22 '10 at 11:59.

You need to implement the getColumnName method in the TableModel interface to return the column names you want. From the Javadoc of TableModel: String getColumnName(int columnIndex) Returns the name of the column at columnIndex. This is used to initialize the table's column header name.

EDIT: The abstract class AbstractTableModel provides implementation for most of the methods in interface TableModel and also provides a default implementation for the getColumnName method in interface TableModel (but which might not suit your purpose as it returns column names as A,B..). Create your own TableModel by subclassing AbstractTableModel and provide implemenation for the abstract methods and override getColumnName method. For example you can try something like: class MyTableModel extends AbstractTableModel { private List rowData; // say private List columnNames; MyTableModel(List data,List names) { rowData = data; columnNames = names; } // provide implementation of abstract methods public int getRowCount() {...} public int getColumnCount() {...} public Object getValueAt(int row, int column) {...} @Override public String getColumnName(int pCol) { return columnNames.

Get(pCol); } ... } // create your table as below; List data = new ArrayList(); data. Add("Test"); data. Add("Try"); List colNames = new ArrayList(); colNames.

Add("Name"); MyTableModel model = new MyTableModel(data,colNames); JTable myTable = new JTable(model).

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.

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.

This is used to initialize the table's column header name. The getColumnName method in interface TableModel (but which might not suit your purpose as it returns column names as A,B..). Create your own TableModel by subclassing AbstractTableModel and provide implemenation for the abstract methods and override getColumnName method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions