Dynamically Creating Menu Items (Part 2)

OK, following on from yesterday, here's the next part. What we want to do is click a button, such as shown below, and, when the button is clicked, cause a new menu item to be added to a predefined menu, in this case the "Shortcuts" menu.

The first step is to define the Shortcuts menu in a layer.xml file:

<folder name="Menu">
    <folder name="Shortcuts"/>
</folder>

Next, we want to populate that folder dynamically, whenever the user clicks the button above. To do that, we need to get hold of the Shortcuts folder within the Menu folder. Then we add in there a new file with ".instance" as the extension, assign our own Action to it, and (as described here, thanks Ernie and the vi/vim project), use the "instanceCreate" and "instanceClass" attributes.

public class SomeTopComponent extends TopComponent {
    private DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    public SomeTopComponent() {
        setDisplayName("Some");
        setLayout(new BorderLayout());
        add(new JButton(new AbstractAction("Add new menu item to shortcuts") {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        final String formatted = formatter.format(System.currentTimeMillis());
                        FileObject menuFolder = FileUtil.getConfigFile("Menu/Shortcuts");
                        try {
                            FileObject newMenu = menuFolder.createData(formatted, "instance");
                            AbstractAction action = new AbstractAction(
                                    "Added at: " + formatted) {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    JOptionPane.showMessageDialog(null, formatted);
                                }
                            };
                            newMenu.setAttribute("instanceCreate", action);
                            newMenu.setAttribute("instanceClass", action.getClass().getName());
                        } catch (IOException ex) {
                            Exceptions.printStackTrace(ex);
                        }
                    }
                });
            }
        }), BorderLayout.NORTH);
    }
}

OK. We now know how to dynamically add a new menu item within a top level menu. But can a top level menu be itself created dynamically too?

That's even simpler:

public class SomeTopComponent extends TopComponent {
    private DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    public SomeTopComponent() {
        setDisplayName("Some");
        setLayout(new BorderLayout());
        add(new JButton(new AbstractAction("Add new menu to main menubar") {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        final String formatted = formatter.format(System.currentTimeMillis());
                        FileObject menuFolder = FileUtil.getConfigFile("Menu");
                        try {
                            menuFolder.createFolder("Added at: " + formatted);
                        } catch (IOException ex) {
                            Exceptions.printStackTrace(ex);
                        }

                    }
                });
            }
        }), BorderLayout.NORTH);
    }
}

The result:


Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
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 2015
SunMonTueWedThuFriSat
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today