Wierd behavior of AbstractAction

Hi,

This is something I came across yesterday. One would assume that following code would create a checkbox with a label and make the checkbox unchecked.

But this is not the case, there is no label on the checkbox.

//Show Preview Checkbox

JCheckBox showPreviewCheck=new JCheckBox("Preview",false);

showPreviewCheck.setBounds(5,5,150,20);

//Toggle panel on/off

showPreviewCheck.setAction(new CheckBoxAction(frame) );

Following fixes the issue;

//Show Preview Checkbox

JCheckBox showPreviewCheck=new JCheckBox("Preview",false);

showPreviewCheck.setBounds(5,5,150,20);

//Toggle panel on/off

showPreviewCheck.setAction(new CheckBoxAction(frame) );

showPreviewCheck.setText("Show preview panel");

It is important that the setText method is called after setAction otherwise it have no effect.

What is happening here, is this a bug or am I duing something wrong here?

[1365 byte] By [gregbugaja] at [2007-11-26 16:05:50]
# 1
> is this a bug or am I duing something wrong here? You are doing something wrong.The Action has a "name" property which is used to set the text of the component. If the value of the property in the Action is null, then your text will also be null (or empty).
camickra at 2007-7-8 22:27:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
That was it, I was not setting the name via the constructor ,Still its surprising that the AbstractAction actually overwritten the JCheckBox label.Thanks
gregbugaja at 2007-7-8 22:27:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

The idea is that you define the Action once, then use it to create one or more buttons or menuitems, and they all pick up the relevant properties from the Action. If you aren't taking advantage of that capability, you might as well use ActionListeners instead of Actions, and attach them with the addActionListener() method instead of setAction().

uncle_alicea at 2007-7-8 22:27:56 > top of Java-index,Desktop,Core GUI APIs...