Home
Categories
Dictionary
Download
Project Details
Changes Log
License

Popup menu



The JEditor.setRightClickPopup(JPopupMenu) allows to add a Popup menu which will appear on a right-click in the JEditor area.

Default popup menu

The DefaultEditorPopup class allows to show by default a Cut/ Copy / Paste and Search options.

For example:
      JEditor editor = new JEditor();
      editor.setRightClickPopup(new DefaultEditorPopup());
will show:
defaultPopup

Customizing the popup

The AbstractContextEditorPopup class allows to create a customized popup menu which may change its content depending on the line and column where the popup is called.

The AbstractContextEditorPopup.createPopupItems(int, int, int, int) will be called by the JEditor when right clicking on the editor area:
  • The first argument is the line (beginning by 0)
  • The second argument is the column (beginning by 0)
  • The third argument is the start offset of the text selection in the Editor
  • The last argument is the end offset of the text selection in the Editor
The method return a boolean specifying if a popup menu should appear.

Examples

The following code will shown a menu presenting the line number and the column:
      JEditor() editor = new JEditor();
      editor.setRightClickPopup(new ContextPopup());      
      
      public class ContextPopup extends AbstractContextEditorPopup {
         public ContextPopup() {
           super("Popup");
         }
      
         @Override
         protected boolean createPopupItems(int line, int column, int startSelection, int endSelection) {
           this.add(new JMenuItem("Line " + (line + 1 ) + " Column " + (column + 1)));
           return true;
         }
      }

contextpopup
The following code will show a menu allowing to insert a text, only if no text is selected:
      JEditor() editor = new JEditor();
      editor.setRightClickPopup(new ContextPopup());      
      
      public class ContextPopup extends AbstractContextEditorPopup {
         public ContextPopup() {
           super("Popup");
         }
      
         @Override
         protected boolean createPopupItems(int line, int column, int startSelection, int endSelection) {
           if (startSelection == endSelection) {
             JMenuItem insertItem = new JMenuItem(new AbstractAction("Insert Text") {
                @Override
                 public void actionPerformed(ActionEvent e) {
                   editor.insertText(startSelection, "The Inserted Text");
                 }
             });
             this.add(insertItem);
             return true;
           } else {
             return false;
           }
         }
      }

Editor defaults

Settings a CodeEditorDefaults.setPopup(JPopupMenu) value for the CodeEditorDefaults class allows to define a Popup which will be shown when right-clicking on the Editor. Any subclass of the AbstractEditorPopup class can be used for the Popup.

Note that setting the value true for the CodeEditorDefaults.hasPopup value will also show the DefaultEditorPopup. For example, the following code will also allow to show the same Popup:
      defaults.hasPopup = true;

      JEditor ta = new JEditor(defaults);

See also


Categories: component

Copyright 2016-2019 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence