Tuesday, May 19, 2009

Changing the Look and Feel of the swing application at runtime

Hi,

I have found an exciting feature to put in the desktop based swing application to change its look and feel on fly with some action associated with some look and feel changing buttons.

You need to create a button and assign an action listener to it. In the "actionPerformed" method of the listener, write down the following code and enjoy the magic...

public void actionPerformed(ActionEvent e) {

try {
UIManager.setLookAndFeel(new MetalLookAndFeel());
Frame frame = WindowManager.getDefault().getMainWindow();
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();

} catch (Exception ex) {
ex.printStackTrace();
}
}


cheers


No comments: