jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes jtable, table model, column headers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jtable, table model, column headers" Watch "jtable, table model, column headers" New topic

jtable, table model, column headers

Dimitris Vorisis

Joined: Jul 29, 2004
Posts: 2
Dear all,

I am using jbuilder 10 for an application and I have bumped on a glitch. I create my own tablemodel (which inherits from the AbstractTableModel) do the necessary work (implementing the abstract methods) and I also override getColumnName method. However, when the application starts no column headers appear (even though when I pass my tabled model in teh JTable constructor getColumnName is called). At first I believed that I had made a mistake with my table model class but when I used the FileTableModel class from O'Rilley's JFC book same thing happened. Does anyone have any idea why this is happening?
thanks in advance


this is my table model
package testapp;

import javax.swing.table.*;
import java.util.Vector;

public class DataModel extends AbstractTableModel {
protected String[] columnNames;
protected Vector data;
protected Class[] columnClasses = new Class[] {String.class, Boolean.class};

public DataModel(Vector data, String[] columnNames) {
this.data = data;
this.columnNames = columnNames;

public int getRowCount() {
return data.size();
public int getColumnCount() {
return columnNames.length;
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return data.elementAt(rowIndex);
case 1:
return (Object) new Boolean(true);
default: return null;

public void removeValueAt(int rowIndex, int columnIndex) {
fireTableRowsDeleted(rowIndex, columnIndex);
public void insertElementAt(Object o, int index){
this.data.insertElementAt(o, index);
fireTableRowsInserted(index, index);

public void append(Object o) {
fireTableRowsInserted(data.size()-1, data.size()-1);

public String getColumnName(int columnIndex) {
return columnNames[columnIndex];

public Class getColumnClass(int col) {
return columnClasses[col];

and this is how i call it from the mainFrame

Vector data = new Vector();
String[] columnNames = new String[] {"column 1", "column 2"};
DataModel dm = new DataModel(data, columnNames)
JTable table = new JTable(dm)
Joe Ess

Joined: Oct 29, 2001
Posts: 9168

The premise behind swing is a separation between data and presentation so your data model will not effect the display of the column header. What does is the component you place the JTable on. Since you didn't show us that particular line, I will tell you the easiest way to display the column header is to add a JTable to a JScrollpane, and add the scroll pane to your JFrame or whatever. See the always-informative Java Tutorial on Tables for more info.

[How To Ask Questions On JavaRanch]
Dimitris Vorisis

Joined: Jul 29, 2004
Posts: 2
yeap that was it. I needed either a scrollpane or explicitly get the headers there. Thanx for the help.
good day
I agree. Here's the link: http://aspose.com/file-tools
subject: jtable, table model, column headers
It's not a secret anymore!