• title: JComponentのKeyBinding一覧を取得する tags: [JComponent, ActionMap, InputMap] author: aterai pubdate: 2008-08-18T11:17:11+09:00 description: JComponentから、ActionMap、InputMapを取得し、KeyBindingの一覧表を作成します。

概要

JComponentから、ActionMapInputMapを取得し、KeyBindingの一覧表を作成します。Miscellaneous Tools - Java Swing UtilitiesMapper.javaを元にUIを変更しています。

サンプルコード

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") {
  @Override 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());
    }
  }
}));
View in GitHub: Java, Kotlin

解説

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

参考リンク

コメント