Sunday Mar 02, 2014

Quick Search Embedded East

The requirement is to have an Action that shows the Quick Search field. One approach was described here and here's another one.

Here's how.

public final class SearchAction implements ActionListener {
    JPanel panel = new JPanel();
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton closeButton = createButton();
        Action qs = Actions.forID(
                "Edit",
                "org.netbeans.modules.quicksearch.QuickSearchAction");
        Component quickSearchToolbarItem
                = ((Presenter.Toolbar) qs).getToolbarPresenter();
        panel.add(quickSearchToolbarItem);
        panel.add(closeButton);
        Frame mainWindow = WindowManager.getDefault().getMainWindow();
        Container contentPane
                = ((JRootPane) mainWindow.getComponents()[0]).getContentPane();
        contentPane.add(BorderLayout.EAST, panel);
        mainWindow.validate();
    }
    private JButton createButton() {
        JButton closeButton = new JButton();
        closeButton.setText("x");
        closeButton.setFocusPainted(false);
        closeButton.setMargin(new Insets(0, 0, 0, 0));
        closeButton.setContentAreaFilled(false);
        closeButton.setBorderPainted(false);
        closeButton.setOpaque(false);
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Frame mainWindow = WindowManager.getDefault().getMainWindow();
                Container contentPane
                        = ((JRootPane) mainWindow.getComponents()[0]).getContentPane();
                panel.removeAll();
                contentPane.remove(panel);
                mainWindow.validate();
            }
        });
        return closeButton;
    }
}
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
« March 2014 »
SunMonTueWedThuFriSat
      
8
9
15
     
Today