• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:ComponentのKeyBinding一覧を取得する
TITLE:JComponentのKeyBinding一覧を取得する
#navi(../)
*ComponentのKeyBinding一覧を取得する [#cb69954b]
*JComponentのKeyBinding一覧を取得する [#cb69954b]
Posted by [[terai]] at 2008-08-18

#contents

**概要 [#g0619870]
Componentから、ActionMap、InputMapを取得し、KeyBindingの一覧表を作成します。[[Mapper.java>ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java]]を改変しています。
JComponentから、ActionMap、InputMapを取得し、KeyBindingの一覧表を作成します。[[Mapper.java>ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java]]を改変しています。

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

#screenshot

**サンプルコード [#a816ad41]
#code{{
final List<Integer> focusType = Arrays.asList(
        JComponent.WHEN_FOCUSED, JComponent.WHEN_IN_FOCUSED_WINDOW,
        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
table.setAutoCreateRowSorter(true);
JPanel p = new JPanel(new GridLayout(2,1,5,5));
p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
p.add(componentChoices);
p.add(new JButton(new AbstractAction("show") {
    public void actionPerformed(ActionEvent e) {
        model.setRowCount(0);
        JComponent c = ((JComponentType)componentChoices.getSelectedItem()).component;
        for(Integer f:focusType) {
            loadBindingMap(f, c.getInputMap(f), c.getActionMap());
        }
    }
}));
}}

**解説 [#i5cf507a]
上記のサンプルでは、JComboBoxで選択されたコンポーネントに割り当てられているデフォルトのKeyBindingを、JTableに表示することができます。

**参考リンク [#md8e3446]
-[[Mapper.java>ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java]]
- [[Mapper.java>ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java]]
- [[How to Use Key Bindings (The Java™ Tutorials)>http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]]

**コメント [#icc0b15a]
- properties.xml の、compile.source を1.5にして、MainPanel.java の table.setAutoCreateRowSorter(true); をコメントアウトすれば、JDK 1.5 でも動作するはず。 -- [[terai]] &new{2008-08-18 (月) 11:17:11};

#comment