2

What I want to achieve is this:

image description

The image means I want to put some button, like JMenuItem, into the application menu bar (JMenuBar) to allow toggling some operation. So I wrote such code:

        // Start button
        JMenuItem startbut = new JMenuItem();
        startbut.setText("Start");
        startbut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ToggleAction();
            }
        });
        menuBar1.add(startbut);

But it doesn't act as expected:

image description

Especially the white background is disturbing and looks broken.

Java GUI libraries have thousands options so I think there must be more valid way to do this. What should I do?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Just a suggestion, what if you use a `JToolBar` adding a `JToggleButton` to it? I wouldn't mess too much with menu bars since these are kind of singular components. – dic19 Feb 28 '15 at 17:53
  • 1
    Man, that's nice! It actually works and I even like that it doesn't look like ordinary menu item: ![screenshot](http://i.stack.imgur.com/hB568.png) – Tomáš Zato Feb 28 '15 at 18:07
  • 1
    @dic19 JMenuBar is container with BoxLayout – mKorbel Mar 01 '15 at 06:34
  • @mKorbel You are right. I was talking about some rendering issues on menus [for example](http://stackoverflow.com/q/28768587/1795530) but of course we can add toggle buttons directly to the menu bar, as I think OP just did. – dic19 Mar 01 '15 at 20:02

1 Answers1

2

Just summarizing the discussion in comments, you can add a JToggleButton to the menu bar instead of adding a JMenuItem, like this:

Action toggleAction = new AbstractAction("Start") {
    @Override
    public void actionPerformed(ActionEvent e) {
        AbstractButton button = (AbstractButton)e.getSource();
        if (button.isSelected()) {
            button.setText("Stop");
            // Start the action here
        } else {
            button.setText("Start");
            // Stop the action here
        }
    }
};

JMenuBar menuBar = new JMenuBar();
menuBar.add(new JMenu("Settings"));
menuBar.add(new JToggleButton(toggleAction));

As @mKorbel pointed out the menu bar is a container so we can add components to it, not just JMenu.

JToggleButton in a JMenuBar

On the other hand using JToolBar instead of menu bar is another option. From an end-user point of view buttons placed in a tool bar are more natural than those placed in a menu bar so it might worth to consider this approach. Something like this will make it:

JToolBar toolBar = new JToolBar();
toolBar.add(new JButton("Settings"));
toolBar.add(new JToggleButton(toggleAction)); // Here 'toggleAction' is the same than the previous one

This snippet will produce an outcome similar to the menu bar:

JToggleButton in a JToolBar

Note

Please note that menu bars has to be added to the top-level container (JFrame or JDialog) using the appropriate setJMenuBar(...) method. This is different than JToolBar which has to be added to the content pane like any other component. See this related topic.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69