You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在mouseReleased中MouseEvent.getClickCount返回0时,能否默认点击次数为1?

关于JTable复选框点击时mouseReleased中clickCount为0的疑问

2019年我提交了JDK-8218421这个bug,之后完全忘记了,至今它仍处于未解决状态。

在Java 17.0.2环境下测试时,以下代码多数情况符合预期:点击表格行的复选框一次,MouseAdapter.mouseReleased方法会打印MouseEvent.getClickCount的结果为1。

但在部分机器上,该方法经常打印0,这让我怀疑是存在bug,或是我对相关内容的理解有误。我的预期是mouseReleased中的点击计数应始终≥1,这个预期是否存在问题?

import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class Test extends JFrame {
   public Test() {
      Object[] cols = {"Id", "Boolean"};
      Object[][] data = {
         {"1", false},
         {"2", false},
         {"3", false},
         {"4", false},
         {"5", false}
      };
      JTable table = new JTable(new DefaultTableModel(data, cols) {
         public Class getColumnClass (int column) {
            return column == 0 ? String.class : Boolean.class;
         }
         public void setValueAt (Object obj, int row, int col) {
            super.setValueAt(obj, row, col);
            System.out.println(String.format("row=%d col=%d", row, col));
         }
      });
      table.addMouseListener(new MouseAdapter() {
         public void mouseReleased (MouseEvent ev) {
            // I argue this should never print 0 but it does sometimes.
            System.out.println("ev.getClickCount() = " + ev.getClickCount());
         }
      });
      table.setPreferredScrollableViewportSize(table.getPreferredSize());
      JScrollPane scrollPane = new JScrollPane(table);
      getContentPane().add(scrollPane);
   }
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            Test frame = new Test();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocation(150, 150);
            frame.setVisible(true);
         }
      });
   }
}

内容的提问来源于stack exchange,提问作者Jeff Holt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:15:10