Java popupmenu in Netbean GUI selectall action

289 views Asked by At

Below is my mouse event code for the clicking action. However this action doesnt work for me. How should i change this to work?

  private void menuSelectallMouseClicked(java.awt.event.MouseEvent evt) {                                           
  menuSelectall.getActionMap().get(DefaultEditorKit.selectAllAction)
  menuSelectall.setText("Select All"); 
  }    
1

There are 1 answers

0
camickr On

You don't use a MouseListener to handle events on a popup menu, the menu item should already be created with the specific Action.

You just create a JMenuItem using the Action from the EditorKit and then you add the menu item to your JPopupMenu. The code should be something like:

JMenuItem selectAll = new JMenuItem( textarea.getActionMap().get(DefaultEditorKit.selectAllAc‌​tion) );
popupMenu.add( selectAll );

The menu item will than handle the mouse event and invoke the Action.