NetBeans Platform Tip: BooleanStateActions
Join the DZone community and get the full member experience.
Join For Free
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
NetBeans
Dialog (software)
Opinions expressed by DZone contributors are their own.
Comments