0

I have an application with a default Look and Feel (insubstantial) and many defaults setted, such like:

    UIManager.put( "List.foreground", Color.BLACK );
    UIManager.put( xyz, zyzLEF );
//around 150 istructions like that

This application it's huge and there are many overrides tailered on client's needs, however now I have the need to create my own JButton with some settings (Foreground, Background, Font and so on) that does not act as I want due to the defaults (as example, setOpaque(true||false) has no effects at all).

So I'm trying to create my own JButton ignoring default look and feel, something like:

public class MyButton extends JButton {
    public MyButton(String text, int lineHeight)
    {
        super(text);
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        setBackground(Color.WHITE);
        //foo
        setOpaque(true);
    }

    @Override
    public String getUIClassID() {
        return "MyButton";
    }

and on startup:

UIManager.put("MyButton","javax.swing.plaf.ButtonUI");

this cause this error:

UIDefaults.getUI() failed: createUI() failed for com.foo.MyButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=null,paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Hello World,defaultCapable=true] java.lang.reflect.InvocationTargetException
java.lang.Error
    at javax.swing.UIDefaults.getUIError(UIDefaults.java:732)
    at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:130)

I bet delegation to UIManger is wrong, but I can't get how to ignore current defaults for a custom item. Any idea?

Thanx in advice

MrPk
  • 2,862
  • 2
  • 20
  • 26
  • 1
    Why is `MyButton`'s class constructor named `JChatButton(String text, int lineHeight)`? – ahoxha Mar 01 '17 at 11:57
  • @A2H miss while changed my code with generic abstracts names. fixed – MrPk Mar 01 '17 at 12:04
  • I tried not overriding `getUIClassID()` or just returning `ButtonUI` and I can see the changes of methods: `setOpaque, setFont, setBackgroun` etc. Isn't this what you are trying to accomplish? – ahoxha Mar 01 '17 at 12:45
  • yes, but If I do what you said, I obtain nothing due to the mentioned overrided in defaults – MrPk Mar 01 '17 at 13:51
  • Correct me if I'm wrong: even though you set the background to WHITE, you don't see a white background? (or font or any other customization that you make to your button). – ahoxha Mar 01 '17 at 16:26
  • @A2H more or less you are right.Default styles are overriden many times and there's behind the scenes a forced merge of custom settings with defaultStyles (to apply automatically gradient and a coherence with existing LookAndFeel). Font color I set it's not what I see as result and so on, so I need to avoid application's default and create a JButton based on basic JButton with swing styles (that has to be javax.swing.plaf.ButtonUI) but I can't get it works. – MrPk Mar 01 '17 at 16:41
  • 1
    From what I understand you're setting some custom `JButton`s with their own L&F and some others to keep the default L&F? Could you create and post a valid [mcve] and an image of actual and desired output please? It would really help me understand what you're trying to achieve. [This question](http://stackoverflow.com/questions/41351173/change-menu-bar-and-menu-items-font/41351323#41351323) is probably related. The `UIManager` doesn't knows about the key `"MyButton"` Here's a [list](http://stackoverflow.com/a/25740576/2180785) of valid keys – Frakcool Mar 01 '17 at 18:30
  • I'm sorry @Frakcool, but mine it's already a minimal and complete example. About "verifiable", I'm just asking how to create a JButton ignoring current Defaults L&F, so no matter what are the current defaults, just ignore them! I have no idea how to reproduce the same behaviour since it comes from a merge of legacy, new and custom libraries/classes. Probably Hunter had the right answer but I have to verify before accept :) – MrPk Mar 01 '17 at 22:18

2 Answers2

2

you can not put this

UIManager.put("MyButton","javax.swing.plaf.ButtonUI");

because static method creatUI() of ButtonUI class (actually it is from ComponentUI class) throws an Error

public static ComponentUI createUI(JComponent c) {
        throw new Error("ComponentUI.createUI not implemented.");
    }

so use javax.swing.plaf.basic.BasicButtonUI

hunter
  • 3,963
  • 1
  • 16
  • 19
1

ignoring current Look and Feel

If I understand your requirement, I think the simplest approach is to override JButton#updateUI() - see the tutorial of close button on tab:

private class TabButton extends JButton implements ActionListener {
    public TabButton() {
        //...
    }

    //we don't want to update UI for this button
    public void updateUI() {
    }
aterai
  • 9,658
  • 4
  • 35
  • 44