Synching Column Move and TableModel

F

farseer

Hi,
Please bear with me as I try to explain thoroughly my problem.
I have Jtable who's model contains a list of objects. these objects
have methods getValue and getPreviousValue, and my model has simplar
methods which simply delegates to the underlying object.
I also have a cell renderer that paints a cell Red if the current value
(TableModel.getValue) is LESS than the previous value (TableModel
..getPreviousValue), and Gray otherwise.
This all works great...UNTIL, I allow column swapping. When I swap
columns the rendering behavior does not "move". To explain what I mean
by this, I'd like to present the code with a use case example.

Now i figured out part of the problem, but the other part i can't
figure out. Below is a code snippet:

public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int col )
{
JComponent c = ( JComponent ) super.
getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col );

if ( value == null )
return c;

c.setBackGround( Color.GRAY );

int oldVal = table.getTableModel().getOldValue( row, col );
int newVal = Integer.parseInt( value.toString() );

if ( oldVal < newVal )
{
c.setBackGround( Color.RED );
}

//DEBUG CODE//
newVal2 = table.getTableModel.getValue( row, col );
System.out.println("col:" + col + ", getPrevVal: " + oldValue +
", getVal: " + newVal2 + ", value:" + value);
////////////////////

Return c;
}


Part of the problem is this. Notice the debug code above. The ouput
and color behavior is correct before swapping:
col:1, getPrevVal: 15, getVal: 10, value:10 (displays as Red)
col:2, getPrevVal: 20, getVal: 25, value:25 (displays as Gray)

The behavior after swapping is incorrect. Notice, the 'value'
parameter is the only thing different. This 'value' parameter is
actually the value that is displayed in the jtable:
col:1, getPrevVal: 15, getVal: 10, value:25 (displays as Gray -
wrong)
col:2, getPrevVal: 20, getVal: 25, value:10 (displays as Red -
wrong)

So as you can see, after swapping, there is a mismatch between the
TableModel and what is being displayed. The row and column passed to
getTableCellRendererComponent still references the original position of
the tableModel. NOW, I understand that structure changes to a table
does not affect the underlying model, but how do I solve my problem?
How do I synch these two?
 
F

farseer

figured it out...fyi:

adding this to my TableCellRenderer does the trick:

int mcol = table.convertColumnIndexToModel( tableCol );
 
F

farseer

Ugh! column swapping works, but now sorting is whacky. haven't had
time to look into detail what is going on..but in short...

-sorting will sort columns correctly, but the next time a cell is
updated, the rows revert to their original order
-rendering is very whacky...colors all over.

ohh boy..
 
F

farseer

I think my problem here is need a convertRowIndexToModel method. anyone
have an idea of how this would be implemented?

i'm guessing this may require the table or model to maintain a map of
row indices to model indices?
 
S

steve

figured it out...fyi:

adding this to my TableCellRenderer does the trick:

int mcol = table.convertColumnIndexToModel( tableCol );


you are also assuming that the objects in the table are only numbers,

int oldVal = table.getTableModel().getOldValue( row, col );
int newVal = Integer.parseInt( value.toString() );


what happends if there are strings in the table.

you need to beef up the routines so that your code is more universal.
 
F

farseer

this particluar cellrenderer only handles numbers as it is expected the
columns it will be attached to will display only numbers.

FYI, the solution to my problem was to maintain a Row To Model index
map. and much like we do with convertColumnToModelIndex, we created a
method on the table called convertRowToModelIndex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top