NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
sandgorgon
Joined: 11 Apr 2012 Posts: 20
|
Posted: Wed Apr 11, 2012 10:49 pm Post subject: Adding a Checkbox Menu Item or a Toggle Action to the Menu |
|
|
Guys,
How can you add a Checkbox Menu Item to a Menu? I've been scouring to almost the butt-end of the Internet and for the life of me I can't find anything that is not dated 2006 at the latest - and it talks about editing layers.xml directly - so there is no friendly IDE way to make menu items of that type?
Currently using NB7.1.1 on a Linux box.
Heeelp ,
Nom |
|
| Back to top |
|
 |
sandgorgon
Joined: 11 Apr 2012 Posts: 20
|
Posted: Thu Apr 12, 2012 1:30 am Post subject: RE: Adding a Checkbox Menu Item or a Toggle Action to the Menu |
|
|
I got this working but being new to using the Platform, I am not sure about the consequences of what I did or if there is an inherent side-effect(s) to how I did it.
I'd appreciate feedback from those more knowledgeable about the details of the framework - please feel free to correct my attempt here if there is some consequence i need to be aware of.
Basically what I did was use the New-Action menu option, then I modified the implementation of the ActionListener magically created by the IDE to this...
| Code: |
@ActionID(category = "Tools", id = "com.micron.bmap.BMapGeneral.Actions.EnableExtDebug")
@ActionRegistration(displayName = "#CTL_EnableExtDebug")
@ActionReferences({
@ActionReference(path = "Menu/Tools", position = 300)
})
@Messages("CTL_EnableExtDebug=Enable Ext. Debug Info")
public final class EnableExtDebug extends BooleanStateAction {
public EnableExtDebug() {
super();
// I want the initial state to be false instead of true.
setBooleanState(false);
}
@Override
public void actionPerformed(ActionEvent e) {
// Just do something to toggle the state for testing purposes...
setBooleanState(!getBooleanState());
System.out.println(getBooleanState());
}
@Override
public String getName() {
return Bundle.CTL_EnableExtDebug();
}
@Override
public HelpCtx getHelpCtx() {
return HelpCtx.DEFAULT_HELP;
}
@Override
public JMenuItem getMenuPresenter() {
return new org.openide.awt.Actions.CheckboxMenuItem(this, false);
}
}
|
|
|
| Back to top |
|
 |
Ernie Rael Posted via mailing list.
|
Posted: Thu Apr 12, 2012 10:48 pm Post subject: [platform-dev] Re: Adding a Checkbox Menu Item or a Toggle Action to the Menu |
|
|
If you want a menu action that persists to Preferences, you can set that up without doing any code. See NetBeans javadocs for org.openide.awt.Actions.checkbox. Note that program interactions with the menu item are done through the Preferences API.
I'm no expert to critique your usage of the annotations. Have you looked at the code for BooleanStateAction? The javadocs indicate:
I don't think you need to override actionPerformed. And since you put all that stuff into the annotations, I don't think you need to override all those other methods.
-ernie
On 4/11/2012 6:31 PM, sandgorgon wrote: | Quote: | | Quote: | I got this working but being new to using the Platform, I am not sure about the consequences of what I did or if there is an inherent side-effect(s) to how I did it.
I'd appreciate feedback from those more knowledgeable about the details of the framework - please feel free to correct my attempt here if there is some consequence i need to be aware of.
Basically what I did was use the New-Action menu option, then I modified the implementation of the ActionListener magically created by the IDE to this...
Code:
@ActionID(category = "Tools", id = "com.micron.bmap.BMapGeneral.Actions.EnableExtDebug")
@ActionRegistration(displayName = "#CTL_EnableExtDebug")
@ActionReferences({
@ActionReference(path = "Menu/Tools", position = 300)
})
@Messages("CTL_EnableExtDebug=Enable Ext. Debug Info")
public final class EnableExtDebug extends BooleanStateAction {
public EnableExtDebug() {
super();
// I want the initial state to be false instead of true.
setBooleanState(false);
}
@Override
public void actionPerformed(ActionEvent e) {
// Just do something to toggle the state for testing purposes...
setBooleanState(!getBooleanState());
System.out.println(getBooleanState());
}
@Override
public String getName() {
return Bundle.CTL_EnableExtDebug();
}
@Override
public HelpCtx getHelpCtx() {
return HelpCtx.DEFAULT_HELP;
}
@Override
public JMenuItem getMenuPresenter() {
return new org.openide.awt.Actions.CheckboxMenuItem(this, false);
}
}
| |
|
|
| Back to top |
|
 |
sandgorgon
Joined: 11 Apr 2012 Posts: 20
|
Posted: Thu Apr 12, 2012 11:32 pm Post subject: |
|
|
Hi Ernie,
I'll take a look at the Preferences API some more - thanks for the advice.
At the minimum, getName() and getHelpCtx() must be overridden accdg. to the compile error I was getting. And an interesting thing as well, if I do not override getMenuPresenter() it does default to a checkbox menu item already.
I didn't try that yesterday.
Thanks for your input.
Regards,
Mon |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|