About DefaultTableCellRenderer
Hi,
I have this class:
Why Iget the color of string name in red only if I selected this row?
lbl.setForeground(Color.Red);
publicclass UsersTableRendererextends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,boolean hasFocus,int row,int column){
JLabel lbl = (JLabel) super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (isSelected){
lbl.setForeground(Color.white);
lbl.setBackground(new Color(0, 0, 139));
}
if (valueinstanceof String){
if(value.equals(UserLogin.getInstance().getFullName())){
lbl.setForeground(Color.Red);
}
return lbl;
}
}
[1479 byte] By [
yael800a] at [2007-11-26 21:28:37]

# 1
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 2
My question is general, why the renderer ignor this line: lbl.setForeground(Color.red);
I get red letters only if I selected this row in a table although I have this:
if (isSelected)
if (isSelected) {
lbl.setForeground(Color.white);
lbl.setBackground(new Color(0, 0, 139));
}
lbl.setForeground(Color.red);
Message was edited by:
yael800
# 3
> My question is general,And my answer is general. We can't problem solve based on a few random lines of code. Your original code has an if condition, so who knows whether the if is executed or not.
# 5
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
class Test extends JFrame
{
public Test()
{
setLocation(400,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTable table = new JTable(2,2);
String[] columnNames = {"Name","Id"};
Object[][] data = new Object[2][2];
data[0][0] = ("Yael");
data[0][1] = ("Dan");
data[1][0] = (1);
data[1][1] = (2);
table = new JTable(new DefaultTableModel(data, columnNames));
table.setDefaultRenderer(Object.class,new changefontColour());
getContentPane().add(new JScrollPane(table));
pack();
}
public static void main (String[] args){new Test().setVisible(true);}
}
class changefontColour extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel lbl = (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (isSelected) {
lbl.setForeground(Color.white);
lbl.setBackground(new Color(0, 0, 139));
}
if(value.equals("Yael"))
{
lbl.setForeground(Color.RED);
}
return lbl;
}
}
I want to get Yael's name in red, but when I'm selected a row, I want to get all data of this selected row (includ Yael) in white.
Thanks :)
# 6
It's just simple logic.
The second if in your renderer is setting the foreground after the isSelected if.
So if you want all the text to be white when the row is seleted simply add else before the second if.
if (isSelected) {
lbl.setForeground(Color.white);
lbl.setBackground(new Color(0, 0, 139));
}
else if(value.equals("Yael"))
lbl.setForeground(Color.RED);
# 7
Thanks :)Message was edited by: yael800
# 8
Once you posted your SSCCE you got an answer withing 5 minutes. Before that you waited two days for help. See how spending that extra 5 minutes to create a SSCCE can lead to a good answer?
# 9
Actually the question here was the problem.
The first question was: "I get red letters only if I selected this row in a table although I have this"
Which is the opposite from the question asked later: "I want to get Yael's name in red, but when I'm selected a row, I want to get all data of this selected row (includ Yael) in white"
So first the OP needs to learn how to ask the correct question and then to post a SSCCE.
# 10
> So first the OP needs to learn how to ask the correct question and then to post a SSCCE.
Agreed, which is why I always recommend a SSCCE. English is not always the first language of people asking the question or answering the question. A SSCCE helps to clarify the question and break the language barrier.
"A picture (the SSCCE), is worth a thousand words".