Monday Feb 10, 2014

Centralized Search Action

Here's how to show the Quick Search textfield in a dialog by clicking a menu item.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JPanel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.awt.Actions;
import org.openide.util.NbBundle.Messages;
import org.openide.util.actions.Presenter;

@ActionID(
        category = "Edit",
        id = "org.bla.SearchAction"
)
@ActionRegistration(
        displayName = "#CTL_SearchAction"
)
@ActionReference(path = "Menu/File", position = 1300)
@Messages("CTL_SearchAction=Search")
public final class SearchAction implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        JPanel panel = new JPanel(new BorderLayout());
        Action qs = Actions.forID("Edit", "org.netbeans.modules.quicksearch.QuickSearchAction");
        Component quickSearchToolbarItem = ((Presenter.Toolbar) qs).getToolbarPresenter();
        panel.add(quickSearchToolbarItem, BorderLayout.CENTER);
        DialogDescriptor dd = new DialogDescriptor(panel, "Search");
        DialogDisplayer.getDefault().notify(dd);
    }
}
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« February 2014 »
SunMonTueWedThuFriSat
      
8
9
15
16
19
22
23
27
 
       
Today