代码之家  ›  专栏  ›  技术社区  ›  stefita

JLabel或JTable单元格上的ActionListener

  •  21
  • stefita  · 技术社区  · 16 年前

    JLabel[][] 作为数据。现在我想检测双击JLabel或表单元格(但仅在其中一列中)。如何在JLabel表格单元格上添加操作/鼠标侦听器?

    2 回复  |  直到 12 年前
        1
  •  65
  •   Vinay Sajip    16 年前

    怎么样:

    table.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
          JTable target = (JTable)e.getSource();
          int row = target.getSelectedRow();
          int column = target.getSelectedColumn();
          // do some action if appropriate column
        }
      }
    });
    
        2
  •  27
  •   camickr    16 年前

    基本上与已经接受的建议相同,除了:

    a) 你应该用鼠标按住鼠标,而不是鼠标滑动。只有在同一像素位置生成mousePressed和MouseRelease事件时,才会触发mouseClicked事件。如果用户在双击时将鼠标移动1个像素,您将无法获得预期的双击效果。