NetBeans Platform Tip: BooleanStateActions
If you want to have an action that changes flag state using toobar button or menu check, you can use BooleanStateActions.
Create ChangeStateAction using wizard dialog with menu and toolbar buttons.

Here is the generated code:
public final class ChangeStateAction extends CallableSystemAction
{
public void performAction()
{
// TODO implement action body
}
public String getName()
{
return NbBundle.getMessage(ChangeStateAction.class, "CTL_ChangeStateAction");
}
@Override
protected String iconResource()
{
return "com/antilogics/binary.png";
}
public HelpCtx getHelpCtx()
{
return HelpCtx.DEFAULT_HELP;
}
@Override
protected boolean asynchronous()
{
return false;
}
}
Replace
CallableSystemAction class with BooleanStateAction, remove
"performAction" and "asynchronous" methods. Add following code:
private JToggleButton toggleButton;
private JCheckBoxMenuItem menuButton;
public ChangeStateAction()
{
// initial state value
setBooleanState(true);
toggleButton = new JToggleButton();
Actions.connect(toggleButton, this);
menuButton = new JCheckBoxMenuItem(getName());
Actions.connect(menuButton, this);
}
@Override
public void actionPerformed(ActionEvent event)
{
super.actionPerformed(event);
boolean state = getBooleanState();
// do something with new state...
}
// replace toolbar button with yours
@Override
public Component getToolbarPresenter()
{
return toggleButton;
}
// replace menu item with yours
@Override
public JMenuItem getMenuPresenter()
{
return menuButton;
}
Here is the result:

That's all! Enjoy
- Login or register to post comments
- 2401 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
Varun Nischal replied on Sun, 2008/07/06 - 2:46pm
What are the contents of XML Layer?
I don't see that file getting modified as shown in the Wizard Modified Files, though it should get modified...
Thanks,
Varun
Konstantin Chikarev replied on Tue, 2008/07/15 - 4:47am
File should be in the list,instead of project.xml
Looks like a bug :)
Varun Nischal replied on Tue, 2008/07/15 - 5:10am
Yeah, maybe its a bug!
<attr name="com-antilogics-testmodule-ChangeStateAction.shadow/PaletteManager" boolvalue="true"/>
Check out the bold text, what exactly does this do?
michaljohn replied on Thu, 2009/06/11 - 5:30am
jiji530 replied on Fri, 2009/06/26 - 8:23pm