Re: JTree forcing node to
To: comp.lang.java.gui
On Aug 22, 11:44 am, Dennis Groenendijk
<coffeebreakN0S...@brains2b.org> wrote:
Hi Lionel,
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
}
I see. Silly me. I had set the look and feel in the first window
assuming that it was set globally from therein. Moving the code to be
the first thing that happens in the Main method solves the problem as
you suggested.
Thanks
Lionel.
P.S., My original solution to set which icon is used extended the
DefaultTreeCellRenderer class. To avoid any unexpected behaviour that
sometimes occurs when sub-classing I change the implementation such
that my custom cell renderer implements TreeCellRender and is a
wrapper around DefaultTreeCellRenderer. It's only one method to be
implemented so it makes it quite simple. Here's the code in case
anyone is interested:
**
* Provides a wrapper around DefaultTreeCellRenderer in order to set
* DirectoryTreeNode nodes with a folder like icon even if they don't
have
* children.
*
* @author Lionel van den Berg.
*/
public class CustomCellRenderer implements TreeCellRenderer {
private DefaultTreeCellRenderer defaultRenderer;
/**
* Creates a new instance of CustomCellRenderer.
*/
public CustomCellRenderer() {
defaultRenderer = new DefaultTreeCellRenderer();
}
/**
* Calls the method in the DefaultTreeCellRenderer class but
adjust
* parameters if necessary to create a different icon.
*/
public Component getTreeCellRendererComponent(JTree tree, Object
value,
boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
boolean adjustedLeaf = leaf;
//AbstractTreeNode is my own class, you don't need to
understand it, just know that it is a node.
if (value instanceof AbstractTreeNode) {
AbstractTreeNode node = (AbstractTreeNode)value;
//By sending super class that the node is not a leaf it
will return a
//non-leaf-node icon.
if (node.isDirectoryNode()) {
adjustedLeaf = false;
}
}
return defaultRenderer.getTreeCellRendererComponent(tree,
value, sel, expanded,
adjustedLeaf, row, hasFocus);
}
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24