Author: Zafir Anjum
Change the look and feel at any time by calling UIManager.setLookAndFeel(). Since any previous component will already have the previous we also need to update them so that they use the new look and feel. We use the function SwingUtilities.updateComponentTreeUI(frame) to do that.
try {
UIManager.setLookAndFeel(new com.sun.java.swing.plaf.motif.MotifLookAndFeel());
}
catch (Exception e) {}
SwingUtilities.updateComponentTreeUI(tree.getRootPane());
In the above case we are setting up the Motif L&F. Note that the desired L&F should be installed for this to work.
Here's a sample program.
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Tree
{
static JTree tree ;
public static void main(String[] args)
{
JFrame frame = new JFrame("Tree");
frame.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}
} );
frame.getContentPane().setLayout( new BorderLayout() );
tree = new JTree();
JScrollPane sp = new JScrollPane( tree );
frame.getContentPane().add( sp, "Center" );
JButton btn = new JButton( "Test");
btn.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{
test();
}
} );
frame.getContentPane().add( btn, "South" );
frame.pack();
frame.show();
}
static boolean angled = true;
static void test()
{
try {
UIManager.setLookAndFeel(new com.sun.java.swing.plaf.motif.MotifLookAndFeel());
}
catch (Exception e) {}
SwingUtilities.updateComponentTreeUI(tree.getRootPane());
tree.repaint();
}
}
Posted On: 21-Feb-1999