Note that there are some explanatory texts on larger screens.

plurals
  1. POTreeCellRenderer: applying different style of the node's text which is depending on the user object's type
    text
    copied!<p>I have a tree with DefaultMutableTreeNodes only. I want to write a TreeCellRenderer that depends on the type of the contained user object. I tried to write a simple example (which doesn't work. That's why I ended up here). On the first level beyond the root node, the types of the user objects are either <code>AANodeUserObject</code>or <code>LocalAANodeUserObject</code>. Both should be rendered with directory icons even though there might be no child. The other feature of this renderer is, that it should render nodes with user object type <code>AANodeUserObject</code> in red color and bold when they are not "current" (in my example, this is always <code>true</code>). Here is my code:</p> <pre><code> public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) { super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); // Precondition für Default TreeNode Object userObject = ((DefaultMutableTreeNode) value).getUserObject(); if (userObject instanceof TestTree.AANodeUserObject || userObject instanceof TestTree.LocalAANodeUserObject) { if (expanded) { setIcon(openIcon); } else { setIcon(closedIcon); } } if (userObject instanceof TestTree.AANodeUserObject) { TestTree.AANodeUserObject aAnode = (TestTree.AANodeUserObject) userObject; if (!mVersionInfo.get(aAnode)) { renderOutdatedAaNode(); } else { renderDefault(); } } else { renderDefault(); } return this; } private void renderDefault() { setTextNonSelectionColor(cColorBlack); setTextSelectionColor(cColorBlack); setFont(getFont().deriveFont(Font.PLAIN)); } private void renderOutdatedAaNode() { setTextNonSelectionColor(cColorRed); setTextSelectionColor(cColorRed); setFont(getFont().deriveFont(Font.BOLD)); } </code></pre> <p>You can see the result on the attached picture<img src="https://i.stack.imgur.com/ldbVl.png" alt="The resulting tree">. </p> <p>What irritates me is that the directory-icon-thing works fine, the text style doesn't: only the second node (Hashmap value is <code>false</code>) AANodeUserObject should be red and nothing else. What am I doing wrong? Can somebody help? Thanks! Mathias</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload