Skip to main content

APIzation: Replication Package

[Q#1692677][A#1693326] How to create a JButton with a menu?

https://stackoverflow.com/q/1692677

I want to create a Toolbar in my application. If you click a button on that toolbar, it will pop up a menu, just like in Eclipse's toolbar. I don't know how to do this in Swing. Can someone help me please? I've tried Google but found nothing.

Answer

https://stackoverflow.com/a/1693326

This is way harder in Swing than it needs to be. So instead of pointing you to tutorials I've created a fully working example.

APIzation

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ToolbarDemo {

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setPreferredSize(new Dimension(600, 400));
        final JToolBar toolBar = new JToolBar();

        //Create the popup menu.
        final JPopupMenu popup = new JPopupMenu();
        popup.add(new JMenuItem(new AbstractAction("Option 1") {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Option 1 selected");
            }
        }));
        popup.add(new JMenuItem(new AbstractAction("Option 2") {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Option 2 selected");
            }
        }));

        final JButton button = new JButton("Options");
        button.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
        toolBar.add(button);

        frame.getContentPane().add(toolBar, BorderLayout.NORTH);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
package com.stackoverflow.api;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class Human1693326 {

  public static void createJbuttonWithMenu(
    Dimension dimension,
    String buttonText,
    String optionName1,
    final String message1,
    String optionName2,
    final String message2
  ) {
    final JFrame frame = new JFrame();
    frame.setPreferredSize(dimension);
    final JToolBar toolBar = new JToolBar();

    //Create the popup menu.
    final JPopupMenu popup = new JPopupMenu();
    popup.add(
      new JMenuItem(
        new AbstractAction(optionName1) {
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(frame, message1);
          }
        }
      )
    );
    popup.add(
      new JMenuItem(
        new AbstractAction(optionName2) {
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(frame, message2);
          }
        }
      )
    );

    final JButton button = new JButton(buttonText);
    button.addMouseListener(
      new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
          popup.show(e.getComponent(), e.getX(), e.getY());
        }
      }
    );
    toolBar.add(button);

    frame.getContentPane().add(toolBar, BorderLayout.NORTH);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

package com.stackoverflow.api;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

/**
 * How to create a JButton with a menu?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/1693326">https://stackoverflow.com/a/1693326</a>
 */
public class APIzator1693326 {

  public static void createJbutton() {
    final JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(600, 400));
    final JToolBar toolBar = new JToolBar();
    // Create the popup menu.
    final JPopupMenu popup = new JPopupMenu();
    popup.add(
      new JMenuItem(
        new AbstractAction("Option 1") {
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(frame, "Option 1 selected");
          }
        }
      )
    );
    popup.add(
      new JMenuItem(
        new AbstractAction("Option 2") {
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(frame, "Option 2 selected");
          }
        }
      )
    );
    final JButton button = new JButton("Options");
    button.addMouseListener(
      new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
          popup.show(e.getComponent(), e.getX(), e.getY());
        }
      }
    );
    toolBar.add(button);
    frame.getContentPane().add(toolBar, BorderLayout.NORTH);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}