TreeCellRenderer does not work?

D

David

Hello,
I implemented a TreeCellRenderer which should display user objects
depending on a condition in different colours if they are selected or
not. This is my implementation:

public class CategoryTreeCellRenderer extends DefaultTreeCellRenderer
{
private Logger logger =
Logger.getLogger(CategoryTreeCellRenderer.class);
private DocumentContext original;
private static Color COL_BKG_CONTAIN_SELECT = Color.RED;
private static Color COL_TXT_CONTAIN_SELECT = Color.WHITE;
private static Color COL_BKG_CONTAIN_NONSELECT = Color.BLUE;
private static Color COL_TXT_CONTAIN_NONSELECT = Color.WHITE;
private static Color COL_BKG_ABSENT_SELECT = Color.GREEN;
private static Color COL_TXT_ABSENT_SELECT = Color.WHITE;
private static Color COL_BKG_ABSENT_NONSELECT = Color.WHITE;
private static Color COL_TXT_ABSENT_NONSELECT = Color.BLACK;

public CategoryTreeCellRenderer(DocumentContext original) {
this.original = original;
}

public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf, row,
hasFocus);
DomainModelElement elem =
(DomainModelElement)((DefaultMutableTreeNode)value).getUserObject();
if(elem instanceof Category) {
Category cat = (Category)elem;
logger.debug("original.contains("+cat.getID()+"):"+(original.contains(cat)));
if(original.contains(cat)) {
setBackgroundSelectionColor(COL_BKG_CONTAIN_SELECT);
setTextSelectionColor(COL_TXT_CONTAIN_SELECT);
setBackgroundNonSelectionColor(COL_BKG_CONTAIN_NONSELECT);
setTextNonSelectionColor(COL_TXT_CONTAIN_NONSELECT);
} else {
setBackgroundSelectionColor(COL_BKG_ABSENT_SELECT);
setTextSelectionColor(COL_TXT_ABSENT_SELECT);
setBackgroundNonSelectionColor(COL_BKG_ABSENT_NONSELECT);
setTextNonSelectionColor(COL_TXT_ABSENT_NONSELECT);
}
}
invalidate();
repaint();
return this;
}
}

The problem is, it works fine until the condition
'if(original.contains(cat))' is true for a node. The particular node
will be shown correctly, i.e. blue background, black text, but the
following node (for which the condition does not hold) will be white
text on white background (pretty unhandy, I'd say). Please, can
anybody help?
Thanks beforehand,
David
 
M

Mark Wright

One joyful day (10 Sep 2004 02:04:43 -0700 to be precise),
(e-mail address removed)-aachen.de (David) decided that the Usenet
community would benefit from this remarkable comment:
Hello,
I implemented a TreeCellRenderer which should display user objects
depending on a condition in different colours if they are selected or
not. This is my implementation:
<...>

Why are you calling super.getTreeCellRendererComponent() and throwing
away the result? Usually, you return a new component such as a JPanel
from getTreeCellRendererComponent(), I've never seen anybody return the
renderer itself (not saying that's inherently wrong though).

Maybe you should try returning a new component?

Mark Wright
- (e-mail address removed)

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
===================================================
 
B

Babu Kalakrishnan

David said:
Hello,
I implemented a TreeCellRenderer which should display user objects
depending on a condition in different colours if they are selected or
not. This is my implementation:

public class CategoryTreeCellRenderer extends DefaultTreeCellRenderer
{
private Logger logger =
Logger.getLogger(CategoryTreeCellRenderer.class);
private DocumentContext original;
private static Color COL_BKG_CONTAIN_SELECT = Color.RED;
private static Color COL_TXT_CONTAIN_SELECT = Color.WHITE;
private static Color COL_BKG_CONTAIN_NONSELECT = Color.BLUE;
private static Color COL_TXT_CONTAIN_NONSELECT = Color.WHITE;
private static Color COL_BKG_ABSENT_SELECT = Color.GREEN;
private static Color COL_TXT_ABSENT_SELECT = Color.WHITE;
private static Color COL_BKG_ABSENT_NONSELECT = Color.WHITE;
private static Color COL_TXT_ABSENT_NONSELECT = Color.BLACK;

public CategoryTreeCellRenderer(DocumentContext original) {
this.original = original;
}

public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf, row,
hasFocus);
DomainModelElement elem =
(DomainModelElement)((DefaultMutableTreeNode)value).getUserObject();
if(elem instanceof Category) {
Category cat = (Category)elem;
logger.debug("original.contains("+cat.getID()+"):"+(original.contains(cat)));
if(original.contains(cat)) {
setBackgroundSelectionColor(COL_BKG_CONTAIN_SELECT);
setTextSelectionColor(COL_TXT_CONTAIN_SELECT);
setBackgroundNonSelectionColor(COL_BKG_CONTAIN_NONSELECT);
setTextNonSelectionColor(COL_TXT_CONTAIN_NONSELECT);
} else {
setBackgroundSelectionColor(COL_BKG_ABSENT_SELECT);
setTextSelectionColor(COL_TXT_ABSENT_SELECT);
setBackgroundNonSelectionColor(COL_BKG_ABSENT_NONSELECT);
setTextNonSelectionColor(COL_TXT_ABSENT_NONSELECT);
}
}
invalidate();
repaint();
return this;
}
}

The problem is, it works fine until the condition
'if(original.contains(cat))' is true for a node. The particular node
will be shown correctly, i.e. blue background, black text, but the
following node (for which the condition does not hold) will be white
text on white background (pretty unhandy, I'd say). Please, can
anybody help?

First of all no need to call invalidate() or repaint() in your renderer code.
The actuall validation / painting will be done by the UI code after receiving
the component (the result of this method call).

To solve your problem, move the super.getTreeCellRendererComponent call to
*after* customizing the Selectioncolor/nonselectionColor etc. The default
implementation performs the foreground/background selection based on the state
during this call, and switching colors after it has already prepared the
renderer doesn't help.

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

change icon in jtree 0
TreeCellRenderer in Linux 16
Jtree renderers 0
TreeCellRenderer 2
checkbox ploblem pls help!! 15
ploblem in jcheckbox 0
JTable within JTree 1
[JTable]with Jtables in cells 1

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top