Modify JTree look and feel

P

Praetorian

Hi,
I want to modify the look and feel of a JTree (running on Windows) to
the cross platform L&F and am using the following code in my JTree
subclass constructor.

try {
UIManager.setLookAndFeel
( UIManager.getCrossPlatformLookAndFeelClassName() );
SwingUtilities.updateComponentTreeUI( this );
} catch ( Exception e ) {
System.err.println( "L&F exception: " + e.toString() );
}

The problem with this code is that it modifies the global L&F instead
of just that of this JTree instance. I'm loading this JTree into a
MATLAB GUI and since the MATLAB desktop is written in java as soon as
I load this tree the MATLAB desktop colors change too. How can I
change the L&F of the just the JTree instance (all JTrees would be ok
too if I can restore it to the original when this instance is closed).

Thanks in advance,
Ashish.
 
S

Sigfried

Praetorian a écrit :
Hi,
I want to modify the look and feel of a JTree (running on Windows) to
the cross platform L&F and am using the following code in my JTree
subclass constructor.

try {
UIManager.setLookAndFeel
( UIManager.getCrossPlatformLookAndFeelClassName() );
SwingUtilities.updateComponentTreeUI( this );
} catch ( Exception e ) {
System.err.println( "L&F exception: " + e.toString() );
}

The problem with this code is that it modifies the global L&F instead
of just that of this JTree instance. I'm loading this JTree into a
MATLAB GUI and since the MATLAB desktop is written in java as soon as
I load this tree the MATLAB desktop colors change too. How can I
change the L&F of the just the JTree instance (all JTrees would be ok
too if I can restore it to the original when this instance is closed).

JComponent.setUI(ComponentUI) should do the trick
 
P

Praetorian

Praetorian a écrit :






JComponent.setUI(ComponentUI) should do the trick

Sigfried,
Thanks for replying. I should've also added that I started programming
in Java about 3 days ago :). I don't understand how to use the setUI
method. How do I create the ComponentUI object argument for the cross
platform look and feel?

Thank you very much for your help.
 
S

Sigfried

Praetorian a écrit :
Sigfried,
Thanks for replying. I should've also added that I started programming
in Java about 3 days ago :). I don't understand how to use the setUI
method. How do I create the ComponentUI object argument for the cross
platform look and feel?

Thank you very much for your help.

Well, basically, if you want to change the look and feel of a JTree, you
have to set its UI to some subclass of TreeUI, in your case, MetalTreeUI.
 
P

Praetorian

Praetorian a écrit :






Well, basically, if you want to change the look and feel of a JTree, you
have to set its UI to some subclass of TreeUI, in your case, MetalTreeUI.

Sigfried,
Thanks for the reply, this is what I have in my JTree subclass
constructor.

try {
// UIManager.setLookAndFeel
( UIManager.getCrossPlatformLookAndFeelClassName() );
setUI( new javax.swing.plaf.metal.MetalTreeUI() );
SwingUtilities.updateComponentTreeUI( this );
} catch( Exception e ) {
System.err.println( "L&F exception: " + e.toString() );
}

Unfortunately the setUI call doesn't change the L&F, if I comment out
the setUI line and uncomment the UIManager line the L&F changes but
then I have the original problem of it changing the L&F globally
rather than for just this JTree instance. What am I doing wrong?

Ashish.
 
J

John B. Matthews

Praetorian said:
Praetorian a écrit : [...]
Well, basically, if you want to change the look and feel of a JTree, you
have to set its UI to some subclass of TreeUI, in your case, MetalTreeUI.

Sigfried, Thanks for the reply, this is what I have in my JTree
subclass constructor.

try {
// UIManager.setLookAndFeel
( UIManager.getCrossPlatformLookAndFeelClassName() );
setUI( new javax.swing.plaf.metal.MetalTreeUI() );
SwingUtilities.updateComponentTreeUI( this );
} catch( Exception e ) {
System.err.println( "L&F exception: " + e.toString() );
}

Unfortunately the setUI call doesn't change the L&F, if I comment out
the setUI line and uncomment the UIManager line the L&F changes but
then I have the original problem of it changing the L&F globally
rather than for just this JTree instance. What am I doing wrong?

I'd have thought setUI() would suffice, but I see the same result.
UIManager.getUI() reorts the native L&F, while the JComponent says it's
Metal. Invoking updateComponentTreeUI() doesn't seem to matter:

JTree jt = new JTree();
jt.setUI(new javax.swing.plaf.metal.MetalTreeUI());
System.out.println(UIManager.getUI(jt));
System.out.println(jt.getUI());

apple.laf.CUIAquaTree@82c01f
javax.swing.plaf.metal.MetalTreeUI@133796
 
S

Sigfried

John B. Matthews a écrit :
Praetorian said:
Praetorian a écrit : [...]
Well, basically, if you want to change the look and feel of a JTree, you
have to set its UI to some subclass of TreeUI, in your case, MetalTreeUI.
Sigfried, Thanks for the reply, this is what I have in my JTree
subclass constructor.

try {
// UIManager.setLookAndFeel
( UIManager.getCrossPlatformLookAndFeelClassName() );
setUI( new javax.swing.plaf.metal.MetalTreeUI() );
SwingUtilities.updateComponentTreeUI( this );
} catch( Exception e ) {
System.err.println( "L&F exception: " + e.toString() );
}

Unfortunately the setUI call doesn't change the L&F, if I comment out
the setUI line and uncomment the UIManager line the L&F changes but
then I have the original problem of it changing the L&F globally
rather than for just this JTree instance. What am I doing wrong?

I'd have thought setUI() would suffice, but I see the same result.
UIManager.getUI() reorts the native L&F, while the JComponent says it's
Metal. Invoking updateComponentTreeUI() doesn't seem to matter:

JTree jt = new JTree();
jt.setUI(new javax.swing.plaf.metal.MetalTreeUI());
System.out.println(UIManager.getUI(jt));
System.out.println(jt.getUI());

apple.laf.CUIAquaTree@82c01f
javax.swing.plaf.metal.MetalTreeUI@133796

When the MetalTreeUI is installed, all the setting are retrieved by
UIManager, like this:

tree.setBackground(UIManager.getColor("Tree.background"));

So even if the painting is done by a MetalTreeUI, all the settings comes
from the global UIManager...

So if you really want, you could try to overwrite the UIManager tree
setting before calling setUI, and then resetting them to their previous
values.
 
J

John B. Matthews

Sigfried said:
John B. Matthews a écrit :

When the MetalTreeUI is installed, all the setting are retrieved by
UIManager, like this:

tree.setBackground(UIManager.getColor("Tree.background"));

So even if the painting is done by a MetalTreeUI, all the settings comes
from the global UIManager...

Ah, that makes sense. I was seeing native features and (incorrectly)
assumed that nothing had changed. Thanks.
So if you really want, you could try to overwrite the UIManager tree
setting before calling setUI, and then resetting them to their previous
values.

Alternatively, if I want to keep the native L&F and clone a few features
from the cross-platform L&F, I can do something like this:

...
LookAndFeel save = UIManager.getLookAndFeel();
Icon expanded = null;
Icon collapsed = null;
try {
LookAndFeel laf = new javax.swing.plaf.metal.MetalLookAndFeel();
UIManager.setLookAndFeel(laf);
collapsed = (Icon) UIManager.get("Tree.collapsedIcon");
expanded = (Icon) UIManager.get("Tree.expandedIcon");
UIManager.setLookAndFeel(save);
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace(System.err);
}
UIManager.put("Tree.collapsedIcon", collapsed);
UIManager.put("Tree.expandedIcon", expanded);
JTree jt = new JTree();
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top