JTable column names problem

Hi all,

how can I set the column names?

I'm using a TableModel, but as result I get "A", "B" and "C", instead of the column names "Sector", "Stock" and "Stock value".

// This is the "main" GUI class

publicclass GUIextends JPanel{

//...

private JSplitPane createJSplitPane(){

// Creates a JSplitPane: the JTable is in the right side

JTable table =new JTable(new TableModel());

table.setPreferredScrollableViewportSize(new Dimension(200, 200));

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

// Scrollbar appears as needed

JScrollPane right =new JScrollPane(table);

right.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createTitledBorder("Subscribed stocks"),

BorderFactory.createEmptyBorder(2,2,2,2)));

//...

}

}

// This is the table model class

class TableModelextends AbstractTableModel{

/**

*

*/

privatestaticfinallong serialVersionUID = 1;

publicstaticfinal String SECTOR_NAME ="Sector";

publicstaticfinal String STOCK_NAME ="Stock";

publicstaticfinal String STOCK_VALUE ="Stock value";

privatestaticfinalint COLUMNS = 3;

privatestaticint START_NUM_ROWS = 3;

privateint numRows = 0;

privateint nextEmptyRow = 0;

private Vector<Stock> data;

public TableModel(){

data =new Vector<Stock>(10);

}

publicint getRowCount(){

return data.size();

}

publicint getColumnCount(){

return COLUMNS;

}

public Object getValueAt(int rowIndex,int columnIndex){

Stock stock = data.get(rowIndex);

switch (columnIndex){

case 0:

return stock.getSector();

case 1:

return stock.getName();

case 2:

return stock.getValue();

default:

}

return"";

}

@Override

publicboolean isCellEditable(int rowIndex,int columnIndex){

returnfalse;

}

@Override

public Class<?> getColumnClass(int c){

return getValueAt(0, c).getClass();

}

publicsynchronizedvoid updateStock(Stock newValue){

final String name = newValue.getName();

int index = -1;

boolean found =false;

boolean addedRow =false;

int i = 0;

Stock tmp =null;

while (!found && (i < nextEmptyRow)){

tmp = data.get(i);

if (tmp.getName() == name){

found =true;

index = i;

}else{

i++;

}

}

if (found){// Updates old stock

data.set(i, newValue);

}else{// Adds new stock

if (numRows <= nextEmptyRow){

// Adds a row

numRows++;

addedRow =true;

}

index = nextEmptyRow;

data.add(newValue);

}

nextEmptyRow++;

//Notify listeners that the data changed.

if (addedRow){

fireTableRowsInserted(index, index);

}else{

fireTableRowsUpdated(index, index);

}

}

/**

* Clears the table.

*/

publicsynchronizedvoid clear(){

int oldNumRows = numRows;

numRows = START_NUM_ROWS;

data.clear();

nextEmptyRow = 0;

if (oldNumRows > START_NUM_ROWS){

fireTableRowsDeleted(START_NUM_ROWS, oldNumRows - 1);

}

fireTableRowsUpdated(0, START_NUM_ROWS - 1);

}

}//END TableModel

Any idea?

Regards,

Michele

[8110 byte] By [michelemazzuccoa] at [2007-10-2 1:12:45]
# 1
Override getColumnName(...) method of the TableModel.
camickra at 2007-7-15 18:33:31 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks a lot
michelemazzuccoa at 2007-7-15 18:33:31 > top of Java-index,Desktop,Core GUI APIs...