Terai Atsuhiro 2024-01-11 (木) 12:39:26

http://terai.xrea.jp/swing/defaulteditorkit/screenshot.png

private void showPop(final JTextField field, final MouseEvent me) {
  JPopupMenu pop = new JPopupMenu();
  boolean flg = (field.getSelectedText()==null)?false:true;
  Action action = new DefaultEditorKit.CutAction();

  action.setEnabled(flg);
  pop.add(action);
  action = new DefaultEditorKit.CopyAction();

  action.setEnabled(flg);
  pop.add(action);
  action = new DefaultEditorKit.PasteAction();

  pop.add(action);

    public void actionPerformed(ActionEvent ae) {
      field.replaceSelection(null);
    }
  };
  action.setEnabled(flg);
  pop.add(action);
  pop.addSeparator();

    public void actionPerformed(ActionEvent ae) {
      field.cut();
    }
  };
  action.setEnabled(flg);
  pop.add(action);
  pop.show(field, me.getX(), me.getY());
}

*1

http://terai.xrea.jp/swing/defaulteditorkit/screenshot2.png