• category: swing folder: DoubleClick title: JTableのセルをダブルクリック tags: [JTable, MouseListener] author: aterai pubdate: 2004-06-28T05:48:37+09:00 description: JTableのセルをダブルクリックして内容を表示します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTLv3qaXoI/AAAAAAAAAYE/aAnkonlteYo/s800/DoubleClick.png

概要

JTableのセルをダブルクリックして内容を表示します。

サンプルコード

table.setAutoCreateRowSorter(true);
table.addMouseListener(new MouseAdapter() {
  @Override public void mouseClicked(MouseEvent me) {
    if (me.getClickCount() == 2) {
      Point pt = me.getPoint();
      int idx = table.rowAtPoint(pt);
      if (idx >= 0) {
        int row = table.convertRowIndexToModel(idx);
        String str = String.format(
          "%s (%s)", model.getValueAt(row, 1), model.getValueAt(row, 2));
        JOptionPane.showMessageDialog(
          table, str, "title", JOptionPane.INFORMATION_MESSAGE);
      }
    }
  }
});
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JTableMouseListenerを設定し、マウスでのセルのマウスでダブルクリックイベントを取得しています。各セルはクリックで編集状態になってしまわないように、すべて編集不可にしています。

コメント