Swing/DefaultEditorKit のバックアップの現在との差分(No.7)
TITLE:DefaultEditorKitでポップアップメニューからコピー
DefaultEditorKitでポップアップメニューからコピー
編集者:Terai Atsuhiro~
作成日:2005-07-25
更新日:2024-01-11 (木) 12:39:26
概要
DefaultEditorKit
を使って、JTextField
などでポップアップメニューを使ったコピー、貼り付け、切り取りを行います。
Screenshot
Advertisement
概要
DefaultEditorKitを使って、JTextFieldなどでポップアップメニューを使ったコピー、貼り付け、切り取りを行います。サンプルコード
#spanend
#spanadd
// textField.setComponentPopupMenu(new TextFieldPopupMenu());
#spanend
#spanadd
class TextFieldPopupMenu extends JPopupMenu {
#spanend
private final Action cutAction = new DefaultEditorKit.CutAction();
private final Action copyAction = new DefaultEditorKit.CopyAction();
private final Action pasteAction = new DefaultEditorKit.PasteAction();
private final Action deleteAction = new AbstractAction("delete") {
@Override public void actionPerformed(ActionEvent e) {
Component c = getInvoker();
if (c instanceof JTextComponent) {
((JTextComponent) c).replaceSelection(null);
}
}
};
private final Action cut2Action = new AbstractAction("cut2") {
@Override public void actionPerformed(ActionEvent e) {
Component c = getInvoker();
if (c instanceof JTextComponent) {
((JTextComponent) c).cut();
}
}
};
#spandel
#screenshot
#spanend
protected TextFieldPopupMenu() {
super();
add(cutAction);
add(copyAction);
add(pasteAction);
add(deleteAction);
addSeparator();
add(cut2Action);
}
#spandel
**サンプルコード [#mcf85a7d]
#spanend
private void showPop(final JTextField field, final MouseEvent me) {
JPopupMenu pop = new JPopupMenu();
boolean isSelected = (field.getSelectedText()==null)?false:true;
Action action = new DefaultEditorKit.CutAction();
action.putValue(Action.NAME, "切り取り");
action.setEnabled(isSelected);
pop.add(action);
action = new DefaultEditorKit.CopyAction();
action.putValue(Action.NAME, "コピー");
action.setEnabled(isSelected);
pop.add(action);
action = new DefaultEditorKit.PasteAction();
action.putValue(Action.NAME, "貼り付け");
pop.add(action);
action = new AbstractAction("削除") {
public void actionPerformed(ActionEvent ae) {
field.replaceSelection(null);
}
};
action.setEnabled(isSelected);
pop.add(action);
pop.addSeparator();
action = new AbstractAction("切り取り2") {
public void actionPerformed(ActionEvent ae) {
field.cut();
}
};
action.setEnabled(isSelected);
pop.add(action);
pop.show(field, me.getX(), me.getY());
}
@Override public void show(Component c, int x, int y) {
if (c instanceof JTextComponent) {
JTextComponent tc = (JTextComponent) c;
boolean f = tc.getSelectionStart() != tc.getSelectionEnd();
cutAction.setEnabled(f);
copyAction.setEnabled(f);
deleteAction.setEnabled(f);
cut2Action.setEnabled(f);
super.show(c, x, y);
}
}
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin- &jnlp;
- &jar;
- &zip;
解説
-
DefaultEditorKit.CopyAction
をポップアップメニューに追加してクリップボードを使ったコピーなどが可能になるよう設定- サンプルの
cut2
のように、JTextComponent#cut()
メソッドなどを使用する方法もある
- サンプルの
解説
DefaultEditorKitには、エディタとして必要な最小限度の機能をデフォルトで実装されています。上記のサンプルでは、DefaultEditorKit.CopyActionで、システムクリップボードを使ったコピーをポップアップメニューで行っています。- -
- サンプルを
Java Web Start
で起動した場合は、システム全体の共有クリップボードにはアクセス不可能でアプリケーション内部のみでのコピー、貼り付けとなる - ClipboardServiceでシステム全体の共有クリップボードにアクセスする
参考リンク
#screenshot(,screenshot2.png)
コメント
- 「今後この警告を表示しない」をやめて、セキュリティの警告を表示したいが方法が不明。 -- aterai