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
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