JTable's columns rendering/formatting

Hello Everyone,

I'd like to post and ask for advices regarding the correct way to implement a JTable that has for given columns certain rules.

Let us suppose I want to have a table with a date column and two other columns that accepts only Integers and Doubles respectively. These can be modified and must respect the constraints given. Which is the right way to do this?

Moreover I have implemented a TableModel that stores data in a Vector<MyObj>, where MyObj is a object managing (for instance) a date, integer and double.

These are fragment of my code:

publicclass MyTableModelextends AbstractTableModel{

private String[] columnNames ={..};

private Vector<MyTableRow> rowData =new Vector<MyTableRow>();

public MyTableModel(){

rowData.addElement(new MyTableRow(new Date(), 0, 0));

}

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

publicint getRowCount(){return rowData.size();}

publicint getColumnCount(){return columnNames.length;}

public Object getValueAt(int row,int col){return rowData.elementAt(row).get(col);}

}

class MyTableRow{

public MyTableRow(Date date,double a,int b){..}

public Object get(int col){

switch (col){

case 0:return date;

case 1:returnnew Double(a);

case 2:returnnew Integer(b);

default:thrownew IllegalArgumentException("Invalid "

+"access to row data for element at: " + col);

}

}

}

Now, how do I make when a cell is modified to have the correct field of my row object to be modified and how can I implement a "formattedfield" control for my 3 columns?

I have tried to use

getColumnModel().getColumn(5).

setCellRenderer(new IntegerRenderer());

publicclass IntegerRendererextends DefaultTableCellRenderer{

publicvoid setValue(Object value){..}

}

but if this works then it does not to modify my row object.

Please, clarify my doubts if possible.

Thank you very much for any help,

my best regards.

[4387 byte] By [marco87otka] at [2007-11-27 8:25:57]
# 1

Read the tutorial:

Concepts: Editors and Renderers

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender

Using an Editor to Validate User-Entered Text

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#validtext

Also it's better to extend DefaultTableModel, instead of AbstractTableModel.

And if you implement getColumnClass to return the correct class for each column you will get the default editors.

public Class<?> getColumnClass(int columnIndex) {

if (getRowCount() > 0 && getValueAt(0, columnIndex) != null)

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

return super.getColumnClass(columnIndex);

}

You can find camickr's example here:

http://forum.java.sun.com/thread.jspa?threadID=465286&messageID=2147913

Rodney_McKaya at 2007-7-12 20:15:09 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thank you Rodney_McKay for your prompt help, I have understood your techniques!

I have, though, another point that has not been clarified:

where DO I have to update the MyRow object, so that after I modify my cell the changes are not lost?

Thank you very much once more,

my best regards

marco87otka at 2007-7-12 20:15:09 > top of Java-index,Desktop,Core GUI APIs...
# 3

You should also implement setValueAt function to update your Vector.

But if you extend DefaultTableModel and use setValueAt and getValueAt of the default model your question is irrelevant.

Is there a special reason why you need to have your own Vector to store the cell data of the table, and not use the default model data?

Rodney_McKaya at 2007-7-12 20:15:09 > top of Java-index,Desktop,Core GUI APIs...
# 4

My reason for choosing this implementation was that, I will need to add a functionality to add a new row, each time i press a button and this will depend upon my previous row.

I thought it might be simpler to do this by adding a vector-management of the data of the table and having an object representing a row of the table.

Please let me know if you think there is a simpler and more efficient way to do this!

Thank you once more

marco87otka at 2007-7-12 20:15:09 > top of Java-index,Desktop,Core GUI APIs...
# 5
> I will need to add a functionality to add a new rowDefaultTableModel already supports this functionality.
camickra at 2007-7-12 20:15:09 > top of Java-index,Desktop,Core GUI APIs...
# 6

> My reason for choosing this implementation was that, I will need to add a functionality to add a new row, each time i press a button and this will depend upon my previous row.

Just override addRow function of DefaultTableModel and use the selected row with getValueAt to do exactly the same thing.

Rodney_McKaya at 2007-7-12 20:15:09 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thank you very much!
marco87otka at 2007-7-12 20:15:09 > top of Java-index,Desktop,Core GUI APIs...