JTable model's issue  
Author Message
PGR





PostPosted: 2006-1-17 7:52:00 Top

java-programmer, JTable model's issue Hi:

I have got a JTable and its cells are JPanel's (to make Java2D inside)
displaying numbers.

The problem is that I want the cells to be editable, so when you click on
the cell, you could write some number, and the new one be redisplayed.
I wrote a method called setValue() on the AbstractTableModel class which
updates the selected JPanel numerical value, but there is a little problem:
when you click a cell, you get a line full of info about the JPanel, so if
you want to enter a number, you have to clear the cell to avoid getting an
error.

so the question is, is there a way to avoid the info to get displayed? note
that my AbstractTableModel getValue() method needs to return a JPanel..

Thanks !!

--

PGR
 
hiwa





PostPosted: 2006-1-17 12:35:00 Top

java-programmer >> JTable model's issue You must implement a custom table cell editor.

 
Thomas Fritsch





PostPosted: 2006-1-21 0:08:00 Top

java-programmer >> JTable model's issue PGR wrote:
> I have got a JTable and its cells are JPanel's (to make Java2D inside)
> displaying numbers.
>
> The problem is that I want the cells to be editable, so when you click on
> the cell, you could write some number, and the new one be redisplayed.
> I wrote a method called setValue() on the AbstractTableModel class which
> updates the selected JPanel numerical value, but there is a little problem:
> when you click a cell, you get a line full of info about the JPanel,
I assume you talk about a lengthy string like
javax.swing.JPanel[name=bla,x=123,y=456,...]
This is the String returned by JPanel's toString() method.
> so if
> you want to enter a number, you have to clear the cell to avoid getting an
> error.
>
> so the question is, is there a way to avoid the info to get displayed? note
> that my AbstractTableModel getValue() method needs to return a JPanel..

It is usually a bad idea to have UI components (like JPanel) in your
TableModel, because this gives exactly that wierd behavior described
above. Instead, you should have pure data-objects (like Integer, Float,
String, Boolean, ...) there.
Therefore I recommend that you rethink and rework your design:
Don't put a JPanel into your AbstractTableModel with setValue(), but do
so with some kind of Number object (probably java.lang.Integer,
java.lang.Float, or similarly, depending on your purpose).
Don't return a JPanel from your AbstractTableModel.getValue(), but
return a Number object.

Going further, you probably should override the getColumnClass() method
of your AbstractTableModel by something like this:
public Class getColumnClass(int columnIndex) {
if (columnIndex == 2 /*index of your numerical column*/ )
return Integer.class; /*or similar*/
else
return Object.class;
}

When editing in a table cell, the table will behave much smarter then.

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')