Home arrow static arrow Java Programming [Archive] - JTable Problem ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - JTable Problem ?
This topic has 6 replies on 1 page.

Posts:100
Registered: 7/11/03
JTable Problem ?  
Jun 30, 2004 1:48 AM



 
Hi all, I am getting one problem with JTable scorll bar

the problem is : In my JTable I put the different colors for rows to identify
according to the values in the table but if scroll up and down the scroll bar..
the colors of the rows are mixed up...

so could you please tell me if there is any method to solve this problem..

thanks
 

Posts:580
Registered: 8/21/02
Re: JTable Problem ?  
Jun 30, 2004 2:01 AM (reply 1 of 6)



 
In my JTable I put the different
colors for rows to identify
according to the values in the table

How do you do that? The correct way is through a custom TableCellRenderer. If this is how you do it and still have problems, post the code of the renderer.
 

Posts:100
Registered: 7/11/03
Re: JTable Problem ?  
Jun 30, 2004 4:20 AM (reply 2 of 6)



 
The code is like below...
I used colors for different process

TableCellRenderer greyWhiteRenderer = new TableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int col) {
Color[] color = new Color[4];
color[0] = Color.getHSBColor(173,49,94);
color[1] = Color.cyan;
color[2] = Color.blue;
color[3] = Color.getHSBColor(7,50,98);

int color_found = 0;
JLabel label = new JLabel();

//for the normal behaviour
label.setOpaque(true);

ODispatch od = (ODispatch) vDispatch.elementAt(row);

if (row == 0) {
od.color = 0;
} else {
ODispatch od_last = (ODispatch) vDispatch.elementAt(row -
1);

if (od.process.equals(od_last.process)) {
color_found = od_last.color;
} else {
if (od_last.color == 0) {
color_found = 1;
}

if (od_last.color == 1) {
color_found = 0;
}
}

od.color = color_found;
}

//normal stuff
String text = (value == null) ? "" : value.toString();
label.setText(text);
label.setBackground(color[color_found]);

return label;
}
};

 

Posts:580
Registered: 8/21/02
Re: JTable Problem ?  
Jun 30, 2004 5:21 AM (reply 3 of 6)



 
I've tried your code and had the same effects, but i can't see anything wrong with your code. Maybe you should try to post your problem to the Swing forum?
 

Posts:31,095
Registered: 4/30/99
Re: JTable Problem ?  
Jun 30, 2004 8:11 AM (reply 4 of 6)



 
You have your renderer trying to set the colour for cells based on... well, I don't know what, but that's likely to lead to problems if cells aren't rendered from top to bottom. Perhaps you could do a little bit more analysis and come up with a description of how cells are supposed to be coloured. Most likely (or hopefully) your description won't require cell A's colour to be changed just because cell B is being rendered.
 

Posts:5,627
Registered: 3/22/04
Re: JTable Problem ?  
Jun 30, 2004 8:28 AM (reply 5 of 6)



 
At this of being yelled at by a certain professor, here is a table with alternating gray and white rows.

import java.awt.Color;import javax.swing.JTable;import javax.swing.table.*; /** * GrayWhiteTable renders its rows with a sequential color combination of white * and gray. Every row with an even index is rendered white and all those rows * with an odd index are painted light gray. * Note: Do not use GrayWhiteTable for tables with costum renderers such as * check boxes. Use JTable instead and modify DefaultTableCellRenderer. Just keep * in mind that in order to display a table with more than 1 row colors, you * must have 2 separate intances of the renderer, one for each color. */ public class GrayWhiteTable extends JTable{     private DefaultTableCellRenderer whiteRenderer;    private DefaultTableCellRenderer grayRenderer;     public GrayWhiteTable() {        super();    }     public GrayWhiteTable(TableModel dm) {        super(dm);    }     public GrayWhiteTable(Object[][] data, Object[] columns)    {       super (data, columns);    }     public GrayWhiteTable(int rows, int columns)    {       super (rows, columns);    }     /**     * If row is an even number, getCellRenderer() returns a DefaultTableCellRenderer     * with white background. For odd rows, this method returns a DefaultTableCellRenderer     * with a light gray backgroundwiki/./.     */    public TableCellRenderer getCellRenderer(int row, int column) {        if (whiteRenderer == null) {            whiteRenderer = new DefaultTableCellRenderer();        }        if (grayRenderer == null) {            grayRenderer = new DefaultTableCellRenderer();            grayRenderer.setBackground(Color.lightGray);        }         if ( (row % 2) == 0 )            return whiteRenderer;        else            return grayRenderer;    }}
 

Posts:5,627
Registered: 3/22/04
Re: JTable Problem ?  
Jun 30, 2004 8:29 AM (reply 6 of 6)



 
At this of being yelled

Risk of being
 
This topic has 6 replies on 1 page.