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?
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?