Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a JButton with a menu?

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.

like image 876
Kien Truong Avatar asked Nov 07 '09 10:11

Kien Truong


People also ask

How do I create a JFrame menu?

As the code shows, to set the menu bar for a JFrame , you use the setJMenuBar method. To add a JMenu to a JMenuBar , you use the add(JMenu) method. To add menu items and submenus to a JMenu , you use the add(JMenuItem) method.

How do I create a Swing menu?

In order to create menu items in Swing, you need to create new instances of JMenuItem and set different properties for them. You can create menu item with both text and icon. Creates a JMenuItem instance without icon or text. Creates a JMenuItem instance with a given icon.

How do I create a JButton text?

By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.


2 Answers

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.

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);     } } 
like image 71
Steve McLeod Avatar answered Oct 23 '22 08:10

Steve McLeod


I don't see why this is harder than it needs to be or why you should use a MouseListener. The solution by Steve McLeod works, but where the menu appears depends on where the mouse was clicked. Why not just use an ActionListener as normally used for a JButton. It seems neither harder nor less hard.

final JPopupMenu menu = new JPopupMenu(); menu.add(...whatever...);  final JButton button = new JButton(); button.setText("My Menu"); button.addActionListener(new ActionListener() {     public void actionPerformed(ActionEvent ev) {         menu.show(button, button.getBounds().x, button.getBounds().y            + button.getBounds().height);     } }); 

This positions the menu about the same as a menu in a JMenuBar for me, and the position is consistent. You could place it differently by modifying the x and y in menu.show().

like image 39
Kenneth Evans Avatar answered Oct 23 '22 06:10

Kenneth Evans