Hi there.
I have a custom JTree with a custom TreeCellRenderer. My TreeCellRenderer extends the DefaultTreeCellRenderer from swing.
Everything works fine except for one thing, when an Item in my TreeModel changes its name the bounding box of the labels that are used to render the cells in the tree dont change.
To give you an example, when my Item's toString() method returns "This is a long item name" after creation the label with have a rather large bounding box. When I then change the name to "short" the text in the tree correctly updates to the new name, but the bounding box is still the same size.
So when the item is selected in the tree the highlight will be huge.
Even worse is when the name was short at the beginning and became bigger later on. In this case the full name is not shown at all, instead it shows just "...".
Here is the code I use for the TreeCellRenderer:
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { Component result = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); if (result instanceof JLabel) { JLabel label = (JLabel) result; if (value instanceof SourceBrowserNode) { SourceBrowserNode node = (SourceBrowserNode) value; boolean error = node.getSource().getErrors().exists(); boolean changed = node.getSource().hasChanged(); Color textColor = error ? leafColor_error : ( changed ? leafColor_changed : leafColor_normal ); label.setForeground(textColor); if (node.isLeaf()) { Icon icon = error ? leafIcon_error : leafIcon_noError; label.setIcon(icon); } } } return result; }
Heres pictures of the problem in action:
After the item was created:
image1.png
When the name is made smaller afterwards:
image2.png
When the name is made longer afterwards:
image3.png
Thank you very much for the help.