Re: JTree forcing node to have the expand icon



Hi Lionel,

I got around it (with a hack) by extending DefaultTreeCellRenderer and
in the getTreeCellRendererComponent method I do a check to see if it
is a Directory node. If it is I call the parent
getTreeCellRendererComponent with leaf parameter of false which
returns the icon I want.

Your solution is even quicker than the one I posted (unfortunatly the message was
made unreadable by missing returns, this one should be better, but I'm writing this
on an application running in debug mode).

The only problem is that for some really strange reason it is not
using the windows look and feel now. I set the look and feel right at
the start and as it was I had to force the JTree to adopt it, but now
that isn't working either and it is going back to default look and
feel. Any ideas on that one? What I did shouldn't have effected this.

DefaultTreeCellRenderer inherits all settings from UIManager directly (so it inherits
from the LookAndFeel you have set UIManager.setLookAndFeel(..))
It is likely that a LookAndFeel that is installed for the JTree, but not as default does not use
DefaultTreeCellRenderer, but a look-and-feel specific implementation.
To set this right you can either do UIManager.setLookAndFeel(..) to windows at the start
of your application:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
//handle Error
}
or overwrite the constructor of DefaultTreeCellRenderer and set the values yourself
from your look-and-feel. Look at the constructor for DefaultTreeCellRenderer for
the entire list.

Hope this helps.

Dennis..
.