Archive

Archive for the ‘Action’ Category

Recipe 15: How to add a toggle action

February 2, 2015 Leave a comment

Problem: How can I add a toggle button as an action? I tried to add a JToggleButton but it doesn’t work.

Solution: JToggleButton doesn’t really work with the NetBeans toolbar, instead use BooleanStateAction. Here are the steps to follow:

  1. Create a new Action, e.g.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Options",
id = "todo.controller.options.QuickFilterAction")
@ActionRegistration(iconBase = "todo/controller/options/find16.png",
displayName = "#CTL_QuickFilterAction")
@ActionReferences({
    @ActionReference(path = "Menu/Options", position = 50),
    @ActionReference(path = "Toolbars/Options", position = 50),
    @ActionReference(path = "Shortcuts", name = "F3")
})
@Messages("CTL_QuickFilterAction=Quick Filter")
public final class QuickFilterAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        // TODO implement action body
    }
}

2.  Extend BooleanStateAction and since it implements ActionListener this can be deleted from our action:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Options",
id = "todo.controller.options.QuickFilterAction")
@ActionRegistration(iconBase = "todo/controller/options/find16.png",
displayName = "#CTL_QuickFilterAction")
@ActionReferences({
    @ActionReference(path = "Menu/Options", position = 50),
    @ActionReference(path = "Toolbars/Options", position = 50),
    @ActionReference(path = "Shortcuts", name = "F3")
})
@Messages("CTL_QuickFilterAction=Quick Filter")
public final class QuickFilterAction extends BooleanStateAction {

 @Override
 protected void initialize() {
   super.initialize();
   setBooleanState(Settings.isSearchVisible());
 }

 @Override
 public void actionPerformed(ActionEvent e) {
   super.actionPerformed(e);
   TasksTopComponent tasksTopComponent = (TasksTopComponent) WindowManager.getDefault().findTopComponent("TasksTopComponent");
   tasksTopComponent.setSearchVisible(getBooleanState());
 }

 @Override
 public String getName() {
   return "Quick Filter";
 }

 @Override
 public HelpCtx getHelpCtx() {
   return HelpCtx.DEFAULT_HELP;
 }

 @Override
 protected String iconResource() {
   return "todo/controller/options/find16.png";
 }
}

Since, due to lazy initialization, it doesn’t recognize the values passed in the annotations, we need to override the iconResource() method. Because, by default, it is selected, we need to set it to unselected in the initialize() method.

Categories: Action, Filter, Quick Search