TITLE:JPopupMenuをコンポーネントに追加
#navi(../)
*JPopupMenuをコンポーネントに追加 [#nc8dcfd3]
Posted by [[terai]] at 2008-03-10

#contents

**概要 [#d2623802]
JPopupMenuをコンポーネントに追加します。

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#s7d9a0a6]
#code{{
JTextArea textArea = new JTextArea("ComponentPopupMenu Test");
textArea.setComponentPopupMenu(new TextComponentPopupMenu(textArea));
}}
#code{{
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(final JTextComponent textArea) {
    super();
    add(cutAction);
    add(copyAction);
    add(pasteAction);
    addSeparator();
    add(deleteAction = new AbstractAction("delete") {
      public void actionPerformed(ActionEvent evt) {
        textArea.replaceSelection(null);
      }
    });
    addSeparator();
    add(selectAllAction = new AbstractAction("select all") {
      public void actionPerformed(ActionEvent evt) {
        textArea.selectAll();
      }
    });
  }
  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);
  }
}
}}

**解説 [#c9617b81]
上記のサンプルでは、JTextAreaにsetComponentPopupMenu(JPopupMenu)メソッドで、ポップアップメニューを追加しています。

JDK1.5でこのメソッドが追加されたため、各コンポーネントにマウスリスナーを設定して、e.isPopupTrigger()でポップアップを表示するクリックかを判断するといったコードを書く必要が無くなっています。

ポップアップメニューを表示するときに、コンポーネントの状態(例えばJTableやJListなどでの行選択の有無や、テキストが選択されてるかとどうかなどの条件)で、
メニューが実行可か不可かを変更したい場合は、JPopupMenu#show(Component, int, int) メソッドをオーバーライドして使用します。

このサンプルでは、テキストが選択されている場合だけ、カット、コピー、削除メニューが有効になるよう設定しています。

----
以下のように、PopupMenuListener を追加しても、同様の処理((JTabbedPaneなどで、どのタブの上でポップアップが表示されるかなどを取得したい場合[[JTabbedPaneでタブを追加削除>Swing/TabbedPane]]は、この方法だと面倒かも…))を行うことが出来ます。

#code{{
popup.addPopupMenuListener(new PopupMenuListener() {
  public void popupMenuCanceled(PopupMenuEvent e) {}
  public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
  public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
    boolean flg = textArea.getSelectedText()!=null;
    cutAction.setEnabled(flg);
    copyAction.setEnabled(flg);
    deleteAction.setEnabled(flg);
  }
});
}}

**参考リンク [#rd20f140]
-[[JPopupMenuの取得を親に委譲>Swing/InheritsPopupMenu]]
-[[JTabbedPaneでタブを追加削除>Swing/TabbedPane]]

**コメント [#ob667bd4]
- メモ: [[Bug ID: 6675802 Regression: heavyweight popups cause SecurityExceptions in applets>http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6675802]] -- [[terai]] &new{2008-04-05 (土) 20:59:02};
- メモ: [[Bug ID: 6299213 The PopupMenu is not updated if the LAF is changed (incomplete fix of 4962731)>http://bugs.sun.com/view_bug.do?bug_id=6299213]] -- [[terai]] &new{2008-04-10 (木) 18:58:52};

#comment