1

I want to change my componentstyle by using UIManager.

For example:

I click on a Button and the Button foreground changes from black to green. The same for a JCheckbox.....

In my example the changes just work for the Button.gradient.... I get no update for Button.foreground and no update for the JCheckbox!

Here my UIManagerClass:

  package components;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.SwingUtilities;


public class OwnUiManager {

    ButtonDemo Bd;

    OwnUiManager(ButtonDemo aThis) {
        Bd = aThis;
    }

    public void setNormal() {
        ArrayList<Object> gradients = new ArrayList();
        gradients.add(0.3);
        gradients.add(0.0);
        gradients.add(new Color(221, 232, 243));
        gradients.add(new Color(255, 255, 255));
        gradients.add(new Color(184, 207, 229));
        javax.swing.UIManager.put("RadioButton.background", Color.PINK);
        javax.swing.UIManager.put("Button.gradient", gradients);
        javax.swing.UIManager.put("Button.foreground", Color.PINK);
        SwingUtilities.updateComponentTreeUI(Bd);
    }

    public void setNightVision() {
        System.out.println("Tes");
        ArrayList<Object> gradients = new ArrayList();
        gradients.add(0.18f);
        gradients.add(0.17f);
        gradients.add(Color.BLACK);
        gradients.add(Color.DARK_GRAY);
        gradients.add(Color.DARK_GRAY);
        javax.swing.UIManager.put("RadioButton.background", Color.GRAY);
        javax.swing.UIManager.put("Button.gradient", gradients);
        javax.swing.UIManager.put("Button.foreground", Color.red);

        SwingUtilities.updateComponentTreeUI(Bd);
    }
}

and here my Buttondemo/Main-Class:

package components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class ButtonDemo extends JPanel
        implements ActionListener {

    protected JButton b1,b2;
    private JRadioButton b3;

    public ButtonDemo() {

        b1 = new JButton("ON");
        b1.addActionListener(this);
        add(b1);
        b2 = new JButton("OFF");
        b2.addActionListener(this);
        add(b2);
        //For Testing the Style
        b3=new JRadioButton("Test");
        add(b3);
    }

    public void actionPerformed(ActionEvent e) {
        OwnUiManager test = new OwnUiManager(this);
        if (e.getSource().equals(b1)) {
            test.setNormal();
        } else {
            test.setNightVision();
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("ButtonDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ButtonDemo newContentPane = new ButtonDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }


    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
JavaNullPointer
  • 1,199
  • 5
  • 21
  • 43

1 Answers1

3
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • how do I apply the changes in the UIManager? Maybe Checkbox/Radio is not the best Example, how does the UI update if I use UIManager.put("Button.foreground", Color.GREEN), UpdateTreeUI doesnt work here. – JavaNullPointer Jan 29 '13 at 13:28
  • 1) `UpdateTreeUI` is basically for changing whatever in the already visible `Swing GUI` 2) see my posts about [UIManager & Nimbus, there is the same logics](http://stackoverflow.com/search?tab=newest&q=user%3a714968%20[nimbus]), 3) but `Keys are / aren't / could / cound't` be the same or similair, `Keys` could be diferrent for various `L&F`, to check linked post for `UIDeafults` – mKorbel Jan 29 '13 at 13:32
  • I think that you miss `UIManager.put("ButtonUI", OwnUiManager.class.getName());`, where value `OwnUiManager.class.getName()` represents `full_qualified_className` in the `Java Package` – mKorbel Jan 29 '13 at 13:58