Your code has some inefficiencies.  However, a likely cause is that, to determine 
whether a row should be red or not, you search through a list, but you don't stop 
looking once you discover it's in the list of reds.  Comparing strings in a 
linear-access list is still inefficient, but perhaps you should remove the loop and 
just use it's guts with the test unselectedRows.contains( String.valueOf(index) )?  
(Again, this is still inefficient and I don't recommend leaving it this way.)

Rather than searching through the unselectedRows list _during rendering_, is there 
some way the red status of the row could be incorporated into the table model, or if 
not, then stored in a fashion which makes the test "should this row index be red" 
quick?  getListCellRendererComponent will be called every time a row header is drawn!

Joel

-----Original Message-----
From: Aleksandr Kravets [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 29, 2003 5:31 PM
To: [EMAIL PROTECTED]
Subject: ListCellRenderer


Hi,

I have a JTable in which I use JList as row header. I also alternate colors of rows 
between gray and white. Now I need to color some of the rows of JList in red. What I 
have is vector of rows that need to be colored in red, in ListCellRenderer I loop thru 
this vector and compare each element to index value of the renderer, if there is a 
match I set background to red if not color other way. It seems to be working but in a 
strange way. If I have 6 rows and need to color rows 6,4 and 2 the will be only row 6 
colored in red. If I make rows 4 and 2 to be colored in red only row 4 is in red. It 
seems renderer colors only last row to be colored. Don't know what to do. Any help is 
greatly appeciated.

** code below **
thanks,
Alex

class RowHeaderRenderer extends JLabel implements ListCellRenderer {
               RowHeaderRenderer(JTable table) {
                                 JTableHeader header = table.getTableHeader();
                                 setOpaque(true);

setBorder(UIManager.getBorder("TableHeader.cellBorder"));

setHorizontalAlignment(SwingConstants.LEFT);
                                 setForeground(header.getForeground());
                                 setFont(header.getFont());
              }
              public Component getListCellRendererComponent(JList list,
                                                            Object value,
                                                            int index,
                                                            boolean isSelected,
                                                            boolean cellHasFocus){
                     setText((value == null) ? "" : value.toString());
                     for(int i = 0; i < unselectedRows.size(); i++){

if((String.valueOf(index)).equals(unselectedRows.elementAt(i))){
                             System.out.println("UNSELECT = " + 
unselectedRows.elementAt(i));
                             setBackground(new Color(255,51,51));
                             setForeground(white);
                         }
                         else{
                             if(index % 2 != 0){
                                setBackground(white);
                                setForeground(Color.black);
                             }
                             else{
                                 setBackground(gray);
                                 setForeground(Color.black);
                             }
                         }
                     }
                     /*
                     if(index % 2 != 0)
                        setBackground(white);
                     else
                        setBackground(gray);
                     */
                     return this;
              }
        }

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED] http://eos.dk/mailman/listinfo/advanced-swing
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to