1

I have a series of JComponents following a look and feel, I then wish to temporarily set a new look and feel in order to add some new components before reverting it back to the original. The code below shows an example of three JButtons, in which the appearance of 'Button 1' and 'Button 3' should be identical, with 'Button 2' differing. The code supplied does revert the overall look and feel, however the lookandfeeldefaults which are set manually (i.e. font the font) are lost. Cheers.

EDIT: Sorry, the question is slightly misleading: all of the look and feel changes happen before the panel is displayed, as seen in the example code. This question is different from the suggested duplicate as the issue here is re-loading the manually set (UIManager.getLookAndFeelDefaults().put(...)) properties, as opposed to the overall look and feel. The underlying reason for this question is that I need to add a JButton with a filled background colour to a panel, and setBackground sets only the border of a JButton when using the windows look and feel whereas the cross platform look and feel fills the background.

UPDATE: The solution is to use UIManager.getLookAndFeelDefaults().putAll(previousLF); instead of setLookAndFeel. The code has been updated accordingly.

import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class test {
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                //Set initial look and feel.
                try{
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    UIManager.getLookAndFeelDefaults().put("Button.font", new Font("Arial", Font.PLAIN, 20));
                } catch (Exception e){}
                JButton button1 = new JButton("1");

                //change look and feel for button two.
                UIDefaults previousLF = UIManager.getLookAndFeelDefaults();
                try {
                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                    UIManager.getLookAndFeelDefaults().put("Button.background", Color.BLUE);
                } catch (Exception e) {}
                JButton button2 = new JButton("2");

                //Revert look and feel.
                try {
                    UIManager.getLookAndFeelDefaults().putAll(previousLF);
                } catch (Exception e) {}
                JButton button3 = new JButton("3");

                //Add buttons to panel and display for testing purposes.
                JPanel testPanel = new JPanel();
                testPanel.add(button1);
                testPanel.add(button2);
                testPanel.add(button3);
                JOptionPane.showMessageDialog(null, testPanel);
            }
        });
    }
}
Hungry
  • 1,645
  • 1
  • 16
  • 26

2 Answers2

3

.

UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
  • in your case should be used local variable for JButton (instead of JFrame) too (valid for part of JComponents by using MetalLookAndFeel, WindowsLookAndFeel)

  • for example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Hi mKorbel thanks for the response. Since all of the components (regardless of look or feel) are added to the frame before it is displayed, updating the component tree doesn't seem to make a difference; instead I would expect a method along the lines of `UIManager.setLookAndFeelDefaults(...)`, although I cannot seem to find one. – Hungry Sep 03 '14 at 10:38
  • @btrs20 JButton has [arrays of Colors](http://stackoverflow.com/questions/5751311/creating-a-custom-button-in-java-with-jbutton/5755124#5755124), e.g. [List of Colors from JButton](http://stackoverflow.com/a/5845007/714968) defined in javax.swing.plaf.ColorUIResource for key Button.background as Color – mKorbel Sep 03 '14 at 11:30
  • I am a little confused, does this relate to my response to romeo? If so, then are you suggesting that I write my own button class in order to have a filled background colour? – Hungry Sep 03 '14 at 12:14
0

I don't think that PLAF is meant to assign different look and feels do different swing components.

What is the reason you are not using the following?

button2.setBackground(bg);
Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
  • Hi Romeo, unfortunately I have to change the look and feel since setting the background color of a JButton using the windows look and feel only changes the appearance of its border, rather than the entire button. – Hungry Sep 03 '14 at 10:43
  • Ok sorry, fortunately u got better answers then mine ;) – Romeo Kienzler Sep 09 '14 at 05:20