TITLE:JComponentのKeyBinding一覧を取得する

Posted by aterai at 2008-08-18

JComponentのKeyBinding一覧を取得する

JComponentから、ActionMap、InputMapを取得し、KeyBindingの一覧表を作成します。Mapper.javaをベースにしてUIを変更しています。

  • &jnlp;
  • &jar;
  • &zip;
KeyBinding.png

サンプルコード

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());
        }
    }
}));

解説

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

参考リンク

コメント

  • このサンプルは、properties.xml の、compile.source を1.5にして、MainPanel.java の table.setAutoCreateRowSorter(true); をコメントアウトすれば、JDK 1.5 でも動作するはず。 -- aterai