3 Replies Latest reply: Oct 8, 2012 1:39 PM by James_D RSS

    Is there a default ContextMenu?

    edward17
      I notice that when I right click on a textarea, I get a menu of standard items. Is this a contextmenu? can I add to this using getContextmenu?

      I ask because I assumed the answer to be yes but get a null ptr exception when I try to add a menu item. I don't want to lose the default menu items, just want to add an item.
        • 1. Re: Is there a default ContextMenu?
          jsmith
          Yes, there is a default context menu for TextInputControls (and a TextArea is a subclass of TextInputControl).
          The default context menu is defined in the TextInputControlSkin.
          http://hg.openjdk.java.net/openjfx/2.2/master/rt/file/tip/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/TextInputControlSkin.java

          It is weird that you cannot get at the default context menu defined in the skin using the getContextMenu method on TextArea, but that doesn't work, nor does monitoring the contextMenuProperty of the TextArea for changes, as that property always remains null.

          Short of defining a new Skin for TextArea in which you define your own ContextMenu, I don't know how you would modify the ContextMenu contents.
          601     final MenuItem undoMI   = new ContextMenuItem("Undo");
          602     final MenuItem redoMI   = new ContextMenuItem("Redo");
          603     final MenuItem cutMI    = new ContextMenuItem("Cut");
          604     final MenuItem copyMI   = new ContextMenuItem("Copy");
          605     final MenuItem pasteMI  = new ContextMenuItem("Paste");
          606     final MenuItem deleteMI = new ContextMenuItem("DeleteSelection");
          607     final MenuItem selectWordMI = new ContextMenuItem("SelectWord");
          608     final MenuItem selectAllMI = new ContextMenuItem("SelectAll");
          609     final MenuItem separatorMI = new SeparatorMenuItem();
          610 
          611     public void populateContextMenu(ContextMenu contextMenu) {
          612         TextInputControl textInputControl = getSkinnable();
          613         boolean editable = textInputControl.isEditable();
          614         boolean hasText = (textInputControl.getLength() > 0);
          615         boolean hasSelection = (textInputControl.getSelection().getLength() > 0);
          616         boolean maskText = (maskText("A") != "A");
          617         ObservableList<MenuItem> items = contextMenu.getItems();
          618 
          619         if (isEmbedded()) {
          620             items.clear();
          621             if (!maskText && hasSelection) {
          622                 if (editable) {
          623                     items.add(cutMI);
          624                 }
          625                 items.add(copyMI);
          626             }
          627             if (editable && Clipboard.getSystemClipboard().hasString()) {
          628                 items.add(pasteMI);
          629             }
          630             if (hasText) {
          631                 if (!hasSelection) {
          632                     items.add(selectWordMI);
          633                 }
          634                 items.add(selectAllMI);
          635             }
          636             selectWordMI.getProperties().put("refreshMenu", Boolean.TRUE);
          637             selectAllMI.getProperties().put("refreshMenu", Boolean.TRUE);
          638         } else {
          639             if (editable) {
          640                 items.setAll(undoMI, redoMI, cutMI, copyMI, pasteMI, deleteMI,
          641                              separatorMI, selectAllMI);
          642             } else {
          643                 items.setAll(copyMI, separatorMI, selectAllMI);
          644             }
          645             undoMI.setDisable(!getBehavior().canUndo());
          646             redoMI.setDisable(!getBehavior().canRedo());
          647             cutMI.setDisable(maskText || !hasSelection);
          648             copyMI.setDisable(maskText || !hasSelection);
          649             pasteMI.setDisable(!Clipboard.getSystemClipboard().hasString());
          650             deleteMI.setDisable(!hasSelection);
          651         }
          652     }
          • 2. Re: Is there a default ContextMenu?
            James_D
            This has a JIRA: http://javafx-jira.kenai.com/browse/RT-23213

            Basically, the default context menu is created by the skin and (I think) seems to be associated with a non-public-API part of the internal representation of the TextArea. Hence you can't get a reference to the context menu, and indeed if you try to set a new context menu on the TextArea you see a weird double-menu effect.
            • 3. Re: Is there a default ContextMenu?
              edward17
              Ouch. OK, thanks for keeping me straight!