Swing/ComponentPopupMenu のバックアップの現在との差分(No.17)
- category: swing folder: ComponentPopupMenu title: JPopupMenuをコンポーネントに追加 tags: [JPopupMenu, JTextComponent, PopupMenuListener, DefaultEditorKit] author: aterai pubdate: 2008-03-10T01:39:57+09:00 description: コンポーネントに右クリックなどでポップアップするJPopupMenuを追加します。 description: JComponentに右クリックなどでポップアップするJPopupMenuを追加します。 image:
概要
コンポーネントに右クリックなどでポップアップするJPopupMenu
を追加します。
JComponent
に右クリックなどでポップアップするJPopupMenu
を追加します。
Screenshot
Advertisement
サンプルコード
JTextArea textArea = new JTextArea("ComponentPopupMenu Test");
textArea.setComponentPopupMenu(new TextComponentPopupMenu());
#spandel
View in GitHub: Java, Kotlin#spanend
#spanadd
#spanend
class TextComponentPopupMenu extends JPopupMenu {
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;
private final Action selectAllAction;
public TextComponentPopupMenu() {
super();
add(cutAction);
add(copyAction);
add(pasteAction);
addSeparator();
add(deleteAction = new AbstractAction("delete") {
@Override public void actionPerformed(ActionEvent evt) {
((JTextComponent) getInvoker()).replaceSelection(null);
}
});
addSeparator();
add(selectAllAction = new AbstractAction("select all") {
@Override public void actionPerformed(ActionEvent evt) {
((JTextComponent) getInvoker()).selectAll();
}
});
//ActionMap am = textArea.getActionMap();
//add(cutAction = am.get("cut-to-clipboard"));
//add(copyAction = am.get("copy-to-clipboard"));
//add(am.get("paste-from-clipboard"));
//addSeparator();
//add(deleteAction = am.get("delete-next"));
//addSeparator();
//add(am.get("select-all"));
// ActionMap am = textArea.getActionMap();
// add(cutAction = am.get("cut-to-clipboard"));
// add(copyAction = am.get("copy-to-clipboard"));
// add(am.get("paste-from-clipboard"));
// addSeparator();
// add(deleteAction = am.get("delete-next"));
// addSeparator();
// add(am.get("select-all"));
}
#spanadd
#spanend
@Override public void show(Component c, int x, int y) {
JTextComponent textArea = (JTextComponent) c;
boolean flg = textArea.getSelectedText() != null;
cutAction.setEnabled(flg);
copyAction.setEnabled(flg);
deleteAction.setEnabled(flg);
super.show(c, x, y);
}
}
解説
上記のサンプルでは、JTextArea
にsetComponentPopupMenu(JPopupMenu)
メソッドで、ポップアップメニューを追加しています。
JDK 1.5
でこのメソッドが追加されたため、各コンポーネントにマウスリスナーを設定して、e.isPopupTrigger()
でポップアップを表示するクリックかを判断するといったコードを書く必要が無くなりました。
-
JDK 1.5
でこのメソッドが追加されたため、各コンポーネントにマウスリスナーを設定してe.isPopupTrigger()
でポップアップを表示するクリックの判断が不要になった - ポップアップメニューを表示するときにコンポーネントの状態(例えば
JTable
やJList
などでの行選択の有無や、テキストが選択されてるかとどうかなどの条件)でメニューが実行可か不可かを変更したい場合は、JPopupMenu#show(Component, int, int)
メソッドをオーバーライドして使用可能 - このサンプルではテキストが選択されている場合だけカット、コピー、削除メニューが有効になるよう設定
JTable
やJList
などでの行選択の有無や、テキストが選択されてるかとどうかなどの条件)で、メニューが実行可か不可かを変更したい場合は、JPopupMenu#show(Component, int, int)
メソッドをオーバーライドして使用します。
このサンプルでは、テキストが選択されている場合だけ、カット、コピー、削除メニューが有効になるよう設定しています。
JPopupMenu#show(Component, int, int)
のオーバーライドではなく、PopupMenuListener
を使用する方法もあります。
-
JPopupMenu#show(Component, int, int)
のオーバーライドではなく、PopupMenuListener
を使用する方法もある
JPopupMenu popup = new JPopupMenu();
#spandel
final Action cutAction = new DefaultEditorKit.CutAction();
#spanend
#spandel
final Action copyAction = new DefaultEditorKit.CopyAction();
#spanend
#spandel
final Action pasteAction = new DefaultEditorKit.PasteAction();
#spanend
#spandel
final Action deleteAction = new AbstractAction("delete") {
#spanend
#spanadd
Action cutAction = new DefaultEditorKit.CutAction();
#spanend
#spanadd
Action copyAction = new DefaultEditorKit.CopyAction();
#spanend
#spanadd
Action pasteAction = new DefaultEditorKit.PasteAction();
#spanend
#spanadd
Action deleteAction = new AbstractAction("delete") {
#spanend
@Override public void actionPerformed(ActionEvent e) {
((JTextComponent) getInvoker()).replaceSelection(null);
}
};
#spandel
final Action selectAllAction = new AbstractAction("select all") {
#spanend
#spanadd
Action selectAllAction = new AbstractAction("select all") {
#spanend
@Override public void actionPerformed(ActionEvent e) {
JPopupMenu p = (JPopupMenu) e.getSource();
((JTextComponent) p.getInvoker()).selectAll();
}
};
popup.add(cutAction);
popup.add(copyAction);
popup.add(pasteAction);
popup.addSeparator();
popup.add(deleteAction);
popup.addSeparator();
popup.add(selectAllAction);
popup.addPopupMenuListener(new PopupMenuListener() {
@Override public void popupMenuCanceled(PopupMenuEvent e) {}
@Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
@Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
JPopupMenu p = (JPopupMenu) e.getSource();
JTextComponent c = (JTextComponent) p.getInvoker();
boolean flg = c.getSelectedText() != null;
cutAction.setEnabled(flg);
copyAction.setEnabled(flg);
deleteAction.setEnabled(flg);
}
});
textArea.setComponentPopupMenu(popup);
- 注:
PopupMenuEvent
からはマウスでクリックした位置を取得することができない -
PopupMenuEvent
からはマウスでクリックした位置を取得できない- このため、
JTabbedPane
などでどのタブの上でポップアップが表示されるかなどを取得したい場合はJPopupMenu#show(...)
をオーバーライドする必要がある - 参考: JTabbedPaneでタブを追加削除
- JTabbedPaneでタブを追加削除
- このため、
参考リンク
- JComponent#setComponentPopupMenu(JPopupMenu) (Java Platform SE 8)
- JPopupMenuの取得を親に委譲
- JTabbedPaneでタブを追加削除
- Bug ID: 6675802 Regression: heavyweight popups cause SecurityExceptions in applets
- Bug ID: 6299213 The PopupMenu is not updated if the LAF is changed (incomplete fix of 4962731)