How to create a menu-item for GUI application based on Netbeans

Let’s add an item that would have the name and availability depends on something (e.g. logged in user or not)

At first you should create a new Action (it mean menu-item) with type ‘Always Enabled’ via the wizard.
layer.xml will be modified and you can find next definition

<file name=”com-package-menu-LoginItemAction.instance”>
<attr name=”delegate” newvalue=”com.package.menu.LoginItemAction”/>
<attr name=”displayName” bundlevalue=”com.package.menu.Bundle#CTL_LoginItemAction”/>
<attr name=”instanceCreate” methodvalue=”org.openide.awt.Actions.alwaysEnabled”/>
<attr name=”noIconInMenu” boolvalue=”false”/>
</file>

In addition, new class will be created: com.package.menu.LoginItemAction

You can run an application and see the new item.
But how change its?
Let’s modify LoginItemAction (we need to extend it by CallableSystemAction)

public final class LoginItemAction extends CallableSystemAction implements ActionListener {
@Override
public void performAction() {
// do something
}
@Override
public String getName() {
if (AuthClass.isLogin()) {
return “Logout”;
} else {
return “Login”;
}
}
@Override
public boolean isEnabled() {
return AuthClass.canLogin();
}
@Override
protected boolean asynchronous() {
return false;
}
@Override
public HelpCtx getHelpCtx() {
return null;
}
}

Now we need to modify layer.xml (simple to delete useless strings)

<file name=”com-package-menu-LoginItemAction.instance”>
<attr name=”delegate” newvalue=”com.package.menu.LoginItemAction”/>
</file>