Results 1 to 6 of 6
Thread: JTable Row Color
- 01-23-2012, 07:00 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
JTable Row Color
Hi all,
in order to set the background color to each row that has some value (1000 for example) in some colon, i made this DefaultTableCellRenderer :
class RowColorRenderer implements TableCellRenderer {
public static final DefaultTableCellRenderer MyDTCR = new DefaultTableCellRenderer();
public Component getTableCellRendererComponent(JTable TheTable,Object TheValue,boolean isSelected,boolean hasFocus,int row,int column)
{ Component renderer = MyDTCR.getTableCellRendererComponent(TheTable, TheValue, isSelected, hasFocus, row, column);
((JLabel) renderer).setOpaque(true);
Color foreground, background;
if (isSelected) {
foreground = Color.yellow;
background = Color.green;
}
else { if (TheTable.getValueAt(row,7).toString().equals("100 0")) {foreground = Color.blue;
background = Color.red;
}
else {foreground = Color.white;
background = Color.blue;
}
}
renderer.setForeground(foreground);
renderer.setBackground(background);
return renderer;
}
}
But, it does NOT work...
Thanks to anyone tell me what is wrong in the code ....Last edited by ellias2007; 01-23-2012 at 07:26 PM.
- 01-23-2012, 07:06 PM #2
Re: JTable Row Color
Sorry, but you telling us that it does not work is as useful to us as us telling you to fix it is to you. If you want help, you'll have to provide an SSCCE that demonstrates what you're talking about, and you'll have to be more specific about what's not working about it. Does it throw an Exception? Does it not display at all? Does it attain sentience and declare war on humanity?
Hint: Don't use == on Strings. Do a search on these forums or google for why not.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-23-2012, 07:24 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
Re: JTable Row Color
Sorry KevinWorkman... here some details
Simply when i run the code, the background in any row that contains the value 1000 in the colon number 7 in NOT red ...
All the rows have a BLUE background...
i think that the problem is in :
if (TheTable.getValueAt(row,7).toString().equals("100 0"))
many thanks to any help
Sorry again...Last edited by ellias2007; 01-23-2012 at 07:27 PM.
- 01-23-2012, 07:26 PM #4
Re: JTable Row Color
Like I said, don't use == when comparing String. Use the equals() method instead. The == operator, when dealing with Objects, simply compares whether two instances are the same, not whether the values they hold are the same.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-23-2012, 07:28 PM #5
Re: JTable Row Color
Did you not like this answer for some reason?
Crossposted: JTable Row ColorHow to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-23-2012, 10:19 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
JTable Cell Color Follow Drag Event
By joeyvitoro in forum AWT / SwingReplies: 6Last Post: 11-30-2011, 06:36 PM -
How to change color of JTable row having a particular value
By johnt in forum AWT / SwingReplies: 6Last Post: 05-14-2011, 06:48 AM -
How to change color of a cell in a Jtable based on a criteria
By RickAintree in forum New To JavaReplies: 5Last Post: 12-20-2010, 08:31 PM -
Color cell in JTable
By ippacciani in forum AWT / SwingReplies: 3Last Post: 03-25-2009, 11:53 AM -
Color of the focued row in a JTable
By SteM in forum AWT / SwingReplies: 2Last Post: 11-20-2007, 06:55 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks