English 中文(简体)
How to add a JPopupMenu to a JMenuBar?
原标题:

I have an application with a popup menu. I d like to use the popup in the usual way (i.e., it should appear when the user right-clicks anywhere in the window), but I d also like to attach it to the main MenuBar at the top of the window. I m not sure how to do this.

I d thought it would as simple as calling myJMenuBar.add(myPopupMenu) but this doesn t work.

JMenuBar.add() wants a JMenu parameter, not a JPopupMenu.

Does anyone have any suggestions?

最佳回答

Instead of trying to reuse the JPopupMenu object, the best approach would be to encapsulate the actions that the menus perform, and reuse those. The popup would trigger those actions, as would the menu items.

From the Action JavaDoc:

In addition to the actionPerformed method defined by the ActionListener interface, this interface allows the application to define, in a single place:

  • One or more text strings that describe the function. These strings can be used, for example, to display the flyover text for a button or to set the text in a menu item.
  • One or more icons that depict the function. These icons can be used for the images in a menu control, or for composite entries in a more sophisticated user interface.
  • The enabled/disabled state of the functionality. Instead of having to separately disable the menu item and the toolbar button, the application can disable the function that implements this interface. All components which are registered as listeners for the state change then know to disable event generation for that item and to modify the display accordingly.

and

JPopupMenu, JToolBar and JMenu all provide convenience methods for creating a component and setting the Action on the corresponding component. Refer to each of these classes for more information.

问题回答

I had the same issue. A right-mouse-click as well as a top menu with exactly the same (complicated) set of menu items. The Action class is something to consider if you are talking about enablement choices, but it s not dealing with visibility and in my case there was also a dynamic list of entries based on a current selection that I wanted to reuse.

So I ended up implementing a Bridge design pattern (I think) for the methods I actually use (add() and addSeparator()):

  public static class MenuBridge
  {
    private JPopupMenu popupMenu;
    private JMenu menu;

    public MenuBridge(JPopupMenu popupMenu)
    {
      this.popupMenu = popupMenu;
    }

    public MenuBridge(JMenu menu)
    {
      this.menu = menu;
    }

    public void addSeparator()
    {
      if(popupMenu!=null) popupMenu.addSeparator();
      else menu.addSeparator();
    }

    public void add(JMenuItem item)
    {
      if(popupMenu!=null) popupMenu.add(item);
      else menu.add(item);
    }
  }

So then I can write a reusable method that computes the menu items and synchronize my right mouse click with the top-level menu:

public void addTaskMenuItems(DefaultMenu menu, List<MDProcTask> taskList)
{
  ...
  menu.add()/menu.addSeparator()
  ...
}

addTaskMenuItems(new DefaultMenu(popupMenu),taskList);
...
taskMenu.addMenuListener( new MenuListener() {
      public void menuCanceled(MenuEvent menuevent)
      {
      }
      public void menuDeselected(MenuEvent menuevent)
      {
      }
      public void menuSelected(MenuEvent menuevent)
      {
        taskMenu.removeAll();
        addTaskMenuItems( new DefaultMenu(taskMenu),getSelectedTasks());
        taskMenu.revalidate();
      }});




相关问题
auto abbreviating JLabel

Anyone written a JLabel (extension/util/??) that automatically abbreviates the contents depending on it s preferred/visible size? much appreciated.

how can install swing/matisse on eclipse?

i tried this link: http://downloads.myeclipseide.com/downloads/products/eworkbench/discovery-6.0/ but i m fail how is this?

Java JTextField with input hint

I would like to add a hint value to my javax.swing.JTextField. It should look like Firefox rendering of <input type="text" title="bla">. This creates an edit field with the text bla in the ...

Swing Generator

I need to develop some java gui using swing. A few years ago I did develop a bit with swing. But it was pretty exhausting, you see, back than there weren t much tools to help you. But I do believe ...

Create a ImageIcon that is the mirror of another one

I ll like to know if there is a way to create a ImageIcon that is the mirror of another ImageIcon. Searching on Google, I found how to do it by using many AWT libraries. Is there a way to do it with ...

热门标签