TITLE:JComponentのKeyBinding一覧を取得する
#navi(../)
RIGHT:Posted by [[aterai]] at 2008-08-18
*JComponentのKeyBinding一覧を取得する [#cb69954b]
JComponentから、ActionMap、InputMapを取得し、KeyBindingの一覧表を作成します。[[Mapper.java>ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java]]をベースにしてUIを変更しています。

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

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTO1Qi0Y2I/AAAAAAAAAdA/yMsuc2sjSKg/s800/KeyBinding.png)

**サンプルコード [#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]
- [[Miscellaneous Tools - Java Swing Utilities>ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/misc.html]]
-- [[Mapper.java>ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java]]
-- %%元サイト?が何処だったか分からなくなってしまったので、ファイルへの直接リンクになっています。%%
- [http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html How to Use Key Bindings (The Java™ Tutorials)]

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

#comment