• category: swing folder: FunctionKeyStartEditing title: JTableでキー入力によるセル編集自動開始を一部禁止する tags: [JTable, ActionMap, InputMap] author: aterai pubdate: 2011-03-07T15:56:35+09:00 description: JTableのセル編集自動開始をファンクションキーの場合だけ無効にします。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TXR_CO_Z9UI/AAAAAAAAA3I/7_3ml86ybo8/s800/FunctionKeyStartEditing.png

概要

JTableのセル編集自動開始をファンクションキーの場合だけ無効にします。Swing - JTable starts editing when F3 is pressed - howto disable?を参考にしています。

サンプルコード

JTable table = new JTable(model) {
  //Swing - JTable starts editing when F3 is pressed - howto disable?
  //https://community.oracle.com/thread/1350192
  @Override public boolean editCellAt(int row, int column, EventObject e) {
    if (e instanceof KeyEvent) {
      int c = ((KeyEvent) e).getKeyCode();
      if (KeyEvent.VK_F1 <= c && c <= KeyEvent.VK_F21) {
        return false;
      }
    }
    return super.editCellAt(row, column, e);
  }
}
View in GitHub: Java, Kotlin

解説

  • ignore: F1,F4-F7,F9-
    • チェックボックスをチェックしている場合、ファンクションキー(Shift, Ctrl+Functionキーも含む)を押しても、セルの編集が開始されないようにJTable#editCellAt(...)メソッドをオーバーライド

上記のサンプルでは、InputMapActionMapF3キーでbeepが鳴るようにキーストロークとアクションを追加しています。このキーストロークはJTable#editCellAtメソッドでのキー入力チェックとは別に実行されるので、デフォルトのF2キーでのstartEditingF8キーでのfocusHeaderは、JTable#editCellAt(...)の戻り値とは関係なく有効になっています。

  • JTable#processKeyBinding(...)をオーバーライドしてキー入力自体を無視すると、InputMapに追加したキーストロークも除かれるので、これらのアクションも実行されない

参考リンク

コメント