Results 1 to 4 of 4
- 10-18-2009, 07:23 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
Changing JTableModel does not change JScrollpane size
I basically want to let the user filter data within a table and change row colors accordingly.
The data for my table is held within the table model.
Table rows should be colored white or gray. Looking only at a certain column, if the cell's value of the current row is incremented by 1 compared to the previous one the color should switch. I seem to have accomplished this by overriding the prepareRenderer method.
However, the user can decide to filter rows in the table. I have also managed to use a filter for the table that works. However, after filtering, rows color should be rerendered since some rows could be omitted and the policy of one row's color depending on the previous still holds.
This does not happen. Rows retain the color previously assigned to them before filtering.
I read on the net that cell rendering happens only once, when the table is first painted. Therefore, I a filter does NOT invoke the table to rerender it is understandable that rows would retain their color.
Therefore, I tried changing the data itself in the data model and use fireTableDataChanged() so that the entire table would get rerendered.
Well that doesn't solve the problem.
Although the table gets updated, the jscrollpane within which it is placed gets partially updated. If the table gets shortened the top of the table gets updated but the scroll bar does not and when I scroll down I see lines that are not supposed to be there and the table freezes.
I tried using revalidate() and repaint() for the scrollpane as well for its parent but nothing works.
table = new JTable(tableModel);
scrollPane = new JScrollPane();
scrollPane.setViewportView(getJTable());
Help !!!
- 10-18-2009, 10:21 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
> I read on the net that cell rendering happens only once, when the table is first painted.
Not true. Cell are constantly being rerendered. For example when you click on a different row, you need to rerender all the cells in the previous row without the highlight and then rerender all the cell in the current row with the rendering.
The easiest way to force repainting is to just use table.repaint(). If this doesn't work then I would guess your custom code in prepareRenderer() is wrong. Maybe you are still referencing the data in the model and not the data in the table.
Also if you want to change the data in the table, then you can just use table.setModel(...). There is no need to recreate the table and/or scrollpane. Those 3 lines of code you posted don't make any sense.
If you need more help post your SSCCE.
- 10-18-2009, 11:35 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
table = new JTable(tableModel)
//first attempt to color rows with white / gray if the reference residue number is incremented by more than 1
{
private static final long serialVersionUID = 1L;
public Component prepareRenderer(TableCellRenderer renderer,int rowIndex, int colIndex)
{
Component comp = super.prepareRenderer(renderer, rowIndex, colIndex);
//alternating gray and white rows for testing
// if (rowIndex % 6 == 0 && !isCellSelected(rowIndex, colIndex))
// comp.setBackground(new Color(220,220,220));//Color.lightGray);
// else
// comp.setBackground(Color.white);
//
if(rowIndex > 0)
{
int prevResNum = (Integer) table.getModel().getValueAt(rowIndex -1 , referenceResidNumColIndx);
int currResNum = (Integer) table.getModel().getValueAt(rowIndex, referenceResidNumColIndx);
if((currResNum - prevResNum ) > 1 && colIndex == 0)
{
if(currentColor == Color.WHITE)
currentColor = Color.LIGHT_GRAY;
else
currentColor = Color.WHITE;
}
}
Color temp = currentColor;
this.setBackground(currentColor);
return comp;
}
}
;
Is there something obviously wrong with this code?
- 10-19-2009, 02:14 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
Changing size of Array
By ravian in forum New To JavaReplies: 3Last Post: 06-05-2012, 08:17 PM -
Changing default Applet Viewer Size?
By dsym@comcast.net in forum Java AppletsReplies: 6Last Post: 08-27-2010, 06:42 PM -
Changing the stack size
By saif.hakim in forum New To JavaReplies: 1Last Post: 04-26-2009, 06:58 AM -
changing font size
By diggitydoggz in forum New To JavaReplies: 1Last Post: 12-25-2008, 07:48 AM -
Listener for JFrame size change
By Thez in forum AWT / SwingReplies: 10Last Post: 02-14-2008, 03:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks