Swing/SelectAllButton のバックアップソース(No.11)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/SelectAllButton へ行く。
- 1 (2009-06-22 (月) 11:18:08)
- 2 (2009-06-23 (火) 18:10:50)
- 3 (2009-10-02 (金) 19:00:39)
- 4 (2012-01-27 (金) 17:55:32)
- 5 (2013-01-09 (水) 20:46:53)
- 6 (2013-07-26 (金) 01:24:07)
- 7 (2013-10-10 (木) 11:35:14)
- 8 (2013-11-17 (日) 18:49:21)
- 9 (2015-11-01 (日) 22:33:45)
- 10 (2017-04-25 (火) 17:07:12)
- 11 (2018-04-18 (水) 16:16:20)
- 12 (2020-04-11 (土) 17:54:20)
- 13 (2021-10-19 (火) 12:58:36)
--- category: swing folder: SelectAllButton title: JTableを別コンポーネントから操作 tags: [JTable, ActionMap] author: aterai pubdate: 2009-06-22T11:18:08+09:00 description: JTableの全選択や選択された行のコピーをJButtonなどの別コンポーネントから行います。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTSx9pjE3I/AAAAAAAAAjU/kU0UU-PWKSI/s800/SelectAllButton.png --- * 概要 [#summary] `JTable`の全選択や選択された行のコピーを`JButton`などの別コンポーネントから行います。 #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTSx9pjE3I/AAAAAAAAAjU/kU0UU-PWKSI/s800/SelectAllButton.png) * サンプルコード [#sourcecode] #code(link){{ private final JTable table = new JTable(model); private final Action copyAction = new AbstractAction("copy") { @Override public void actionPerformed(ActionEvent e) { e.setSource(table); table.getActionMap().get("copy").actionPerformed(e); } }; }} * 解説 [#explanation] 上記のサンプルでは、`JButton`や`JMenuItem`がクリックされたときに、以下のようにしてフォーカスのない`JTable`の全選択とコピーを行っています。 - `AWTEvent#setSource(Object)`メソッドでイベントを`JTable`に再転送 --`Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JMenuItem cannot be cast to javax.swing.JTable`などの例外が発生しないようにするため - `JTable`から`ActionMap`を取得 - `ActionMap`から`Action`を取得 -- これらの`Action`は`JTable`でコピー(KBD{Ctrl+C})するのと同様 - `ActionListener#actionPerformed(ActionEvent)`メソッドで`Action`を実行 //* 参考リンク [#reference] * コメント [#comment] #comment - タイトルの`typo`を修正 -- &user(aterai); &new{2009-06-23 (火) 18:34:44}; - `Java Web Start`で起動した場合は、 %%コピーできない?%% このサンプル内でコピーした文字列などを外部のアプリケーションにペーストすることはできない(逆も不可)? -- &user(aterai); &new{2009-10-02 (金) 19:05:43}; #comment