Search code examples
javaswingactionnetbeans-7jtoolbar

NetBeans Platform: Toolbar and Actions


I have been trying to solve this for the past 2 days with no luck. I went through pages upon pages of solutions that all look correct, but either my implementation is wrong or they aren't they right solutions.

I have created a new Toolbar called AddEditDelete. Then I proceeded to add Actions to it:

Here is the AddAction.java

@ActionID(category = "Edit",
id = "com.waudware.toolbar.AddAction")
@ActionRegistration(iconBase = "com/demo/toolbar/icons/add.png",
displayName = "#CTL_AddAction")
@ActionReferences({
@ActionReference(path = "Toolbars/AddEditDelete", position = 1),
@ActionReference(path = "Shortcuts", name = "D-A")
})
@Messages("CTL_AddAction=Add")

public final class AddAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        //TODO: Code here
    }
}

I also have EditAction.java and DeleteAction.java - they were all created as "Always Enabled".

What I have been trying to do is this: When you click on the Add button on the toolbar, it will execute the code in the AddAction.java AND disable EditAction.java (Gray out the action buttons so they are unclickable).

After 2 days of trying to figure out how to do this, I am completely lost and almost sure its impossible. NetBeans dev forums have been unhelpful so far.

Edit: My question is quite specific and simple: What would be the correct (even if it is a bad practice) approach to disabling EditAction.java from AddAction.java -So far I have tried using Lookup, CookieSet, Direct calls, Action instantiation, and the only thing I got that was remotely what I wanted is

ToolbarPool.getDefault().findToolbar("AddEditDelete").setEnabled(false);

which hides the whole toolbar, but not individual actions(icons) on it.


Solution

  • See Toolbar.getComponents() instead.

    Component components = ToolbarPool.getDefault().
        findToolbar("AddEditDelete").getComponents();
    for (Component component : components) {
        component.setEnabled(false);
    }