Swing/DoubleClick のバックアップの現在との差分(No.2)
#navi(contents-page-name): No such page: ST
FrontPage
2023-07-11 (火) 09:56:08
概要
JTable
のセルをダブルクリックして内容を表示します。
Screenshot
Advertisement
サンプルコード
#spanend
#spanadd
table.setAutoCreateRowSorter(true);
#spanend
#spanadd
table.addMouseListener(new MouseAdapter() {
#spanend
@Override public void mouseClicked(MouseEvent e) {
if (e.getClickCount() >= 2) {
Point pt = e.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);
}
}
}
#spanadd
});
#spanend
#spanadd
View in GitHub: Java, Kotlinjtable.addMouseListener(new MouseInputAdapter() { public void mouseClicked(final MouseEvent me) { int col = jtable.columnAtPoint(me.getPoint()); if(col==DBLCLICK_COLUMN && me.getClickCount()==2) { SwingUtilities.invokeLater(new Runnable() { public void run() { int row = jtable.rowAtPoint(me.getPoint()); String str = getInfo(model.getTest(row)); JOptionPane.showMessageDialog( JOptionPane.INFORMATION_MESSAGE); } }); jtable.repaint(); }else{ super.mouseClicked(me); } } });
解説
-
JTable
にMouseListener
を設定してMouseEvent#getClickCount()
メソッドでマウスクリック数を取得し、これが2
以上になる場合はセルがダブルクリックされたと判断 -
JTable
のセルはダブルクリックで編集開始がデフォルトなのですべてのセルを編集不可に設定