1

I've just noticed that a JButton does not change it text to the NAME value of the coupled Action object when at least the following 2 things are true:

  • the button's preferredSize property is not the default value.
  • the button's text property is not the default of "" (empty).

It could be that there are other combinations of property values that make the button ignore the name of the action, however when I put one or the other on default, it works.

I'll state the obvious that the action is properly registered, and the hideActionText property is false. Additionally, I should say that I set these properties via the properties panel in the Netbeans GUI designer.

A few google searches brought up nothing. Is this normal behaviour and can it be resolved?

StanislavL
  • 56,971
  • 9
  • 68
  • 98
MarioDS
  • 12,895
  • 15
  • 65
  • 121
  • 2
    See also [*Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?*](http://stackoverflow.com/q/7229226/230513). – trashgod Apr 12 '12 at 00:16
  • @trashgod Well thanks, now I know I should try to avoid preferredsize whenever I can. I use the system L&F so I can't use SizeVariant... – MarioDS Apr 12 '12 at 01:28

1 Answers1

6

I would say two things:

  1. Why do you force the preferredSize of the JButton (otherwise it is just based on the text and icon it contains)?
  2. Why would you set the text of the JButton if you want the one coming from the Action NAME?

I don't see any problems in the described behaviour. If no text is provided, then it defaults to the NAME of the Action, if any. Else, it will be just empty. If you want to force the preferredSize, so be it, you have all rights to do so, under some circumstances.

If you want the Action NAME to appear, simply don't set another "text" value on the JButton.

EDIT


If I set the text but not the preferredSize, I still see the button text (even if the text is too small too fit, you get "..." on your text). Test it for yourself on this SSCCE:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame();
        frame.setTitle("frame");
        final JButton button = new JButton(new AbstractAction("Action text") {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.err.println("Hello");
            }
        });
        button.setText("Button text which is a lot longer");
        button.setPreferredSize(new Dimension(100, 30));
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • The reason for the preferredSize is purely aesthetic and for user experience, say for example that I want it to draw more attention to a user/make it stand out more. The reason for the text is that the buttons should still make sense in the GUI designer. I'll admit that there are easy workarounds, but they shouldn't be necessary in the first place... – MarioDS Apr 11 '12 at 23:31
  • @MarioDeSchaepmeester What exactly is it that you think should be different? I agree that its the expected behavior. Are you saying you would like to assign the button a name and still have it overwrite the name and display the action? – Youssef G. Apr 11 '12 at 23:36
  • OK, I understand you wanting to set a text (for preview in the GUI desogner), but you shouldn't. The GUI designer should rather provide a way to enter an example value that is purely there for showing what the button may contain. I don't see any other "work around" for your issue. – Guillaume Polet Apr 11 '12 at 23:37
  • @YoussefG. That's what I'm saying, and it works if I don't change the preferredSize. – MarioDS Apr 11 '12 at 23:44
  • @GuillaumePolet Netbeans doesn't provide that functionality, that I know of. As I said, setting the text alone, it works. I just don't get why and how the `preferredSize` property should have an influence on whether the button accepts the action text or not. Seems like a flaw in the java API... Surely if only because then the action text **might** not fit in the button. But that's the programmers responsibility. – MarioDS Apr 11 '12 at 23:46
  • @MarioDeSchaepmeester I am kind surprised by your saying that if you set the text on the button but not the preferredSize, then you still see the Action name. This is not what I observe using code (I will post it soon). I would be more suspicious about NetBeans than about Java – Guillaume Polet Apr 11 '12 at 23:49
  • @GuillaumePolet The problem is probably the order in which manual code and the netbeans property panel does things. Maybe if you make a new button and add text as soon as you make it, then add the action aftwerwards... Or the other way around... – MarioDS Apr 12 '12 at 00:08
  • @GuillaumePolet I'm off my programming machine now and I don't have an IDE available atm (don't know how to do it without IDE), but I notice you create the button with the action immediately coupled. I would almost bet that if you only add the action after you set the text, you'll have the same situation as I have... – MarioDS Apr 12 '12 at 00:20
  • @MarioDeSchaepmeester yet it would not explain the behaviour related to the preferredSize, unless by adding the preferredSize you would also modify the order in which Action and text are set. – Guillaume Polet Apr 12 '12 at 00:26
  • @GuillaumePolet I'll make sure to assess in which order netbeans modifies the button, tomorrow. The text, preferredSize and Action properties are all set via the netbeans GUI, and it generates the code. If it is like "set text, set action set preferredsize", I'll try adding the action last and see what it gives. – MarioDS Apr 12 '12 at 00:29