xyz
xyz

Reputation: 31

JAVA set property of JButton() using UI Manager

Hey I want to set property : setContentAreaFilled(false) for jbutton. I am unable to do this using UI Manager:

UIManager.put("Button.contentAreaFilled", false);

But the value is not getting changed . Can anybody please suggest the workaround or flaw in the code.

Upvotes: 3

Views: 1313

Answers (1)

Ansharja
Ansharja

Reputation: 1133

I don't think this is possible using UIManager, i've tested it on my pc (Windows 10, jre1.8.0_131) and that key (Button.contentAreaFilled) does not exist (i also tried using different LookAndFeels).

You can find a list of UIManager keys here.

Also, this link (by Rob Camick) provides a nice GUI to check the keys default value in different LAF installed on your machine (screenshot below).

enter image description here

You can use a factory method to create all the buttons in your GUI, setting content area filled to false inside the method.

EDIT

You can't decide which keys will be used by ComponentUI classes, unless you create your own LookAndFeel.

For example, if you want to see how JPanel default background and other properties are installed, take a look at javax.swing.plaf.basic.BasicPanelUI class, for example:

protected void installDefaults(JPanel p) {
        LookAndFeel.installColorsAndFont(p,
                                         "Panel.background",
                                         "Panel.foreground",
                                         "Panel.font");
        LookAndFeel.installBorder(p,"Panel.border");
        LookAndFeel.installProperty(p, "opaque", Boolean.TRUE);
    }

I would not suggest you to play with this, but if you want you can read:

I have never tried it, but i think it could be very painful.

I highly recommend you to use a static factory method to create your buttons: it will be much more simple and safe, for example:

import javax.swing.JButton;
public class ButtonsFactory
{
    public static JButton createButton (String text) {
        JButton button = new JButton (text);
        button.setContentAreaFilled (false);
        // set any other property ...
        return button;
    }
}

Now you can create every button in your application by simply calling:

JButton button = ButtonsFactory.createButton ("...");

Obviously, you can provide more methods to create a button or another component with different arguments.

Upvotes: 1

Related Questions