JPopupMenu

Setting up a right click menu, or popup menu in Java is a tricky but simple procedure.  You would instantiate JPopupMenu, set JMenuItem, attach an ActionListener, set MouseListener.  The beauty of Java GUI structure is in that the listener can be set anywhere.  Any arbitrary component can implement the listener.  A JFrame can have a JPopupMenu and any component can set the MouseListener that set the popup.  And then any component can process the selected JMenuItem.

Supposing say a JFrame has a JTable and a JTextArea.  You would want different JPopupMenu.  Each component can possess the JMenuPopup of its own, and the menu can invoke the function in JFrame and not only of JTable nor JTextArea.

Which component should have the JPopupMenu (usually the sub-component but can be anywhere) determines what should implements the MouseListener.  The JMenuItem in the JPopupMenu should be 'listened' by an ActionListener that should process the demand.

Here is a project that uses a JPopupMenu.

→ JavaMenuMenu.java

Comments

Popular posts from this blog

Logging your Maven project with Logback

TreeEditor: Customizing Drag and Drop

Spring Tool Suite