JTable鼠标监听器无法正常工作

时间:2013-03-25 11:31:40

标签: java swing concurrency jtable mouselistener

我的JTable(jtblLot)鼠标单击事件不会触发某些时间。主要是频繁点击

以下是鼠标点击事件的代码

 private void jtblLot_MouseClicked(java.awt.event.MouseEvent evt) {       

    int row = jtblLot.rowAtPoint(evt.getPoint()), currId = 0;
    int col = 3;

    lotId = jtblLot.getValueAt(row, col).toString();
    if (jtblLot.getValueAt(row, 1) != null) {
    sizeGrp_up = jtblLot.getValueAt(row, 1).toString();
    } else {
    sizeGrp_up = "0";
    }

    if (jtblLot.getValueAt(row, 4) != null) {
    if (jtblLot.getValueAt(row, 4).toString().compareTo("") !=0)
    {
    currId = Integer.parseInt(jtblLot.getValueAt(row, 4).toString()) - 1;
    }
    } else {
    sizeGrp_up = "0";
    }

    cmbCurrency.setSelectedIndex(currId);
    jlblLotId.setText(lotId);

    // Sets Model For Another JTable(jtblLGP) In My Form Get Data From DB
    getLotGradePriceData();

    //On Click I get The Focus To The Clicked Cell
    int col_ = jtblLot.columnAtPoint(evt.getPoint());
    jtblLot.setCellSelectionEnabled(true);
    jtblLot.changeSelection(row, col_, false, false);
    jtblLot.scrollRectToVisible(new Rectangle(jtblLot.getCellRect(row, col_, true)));

} 

2 个答案:

答案 0 :(得分:5)

如果您需要处理每次点击,我建议您处理mouseReleased而不是mouseClicked

答案 1 :(得分:1)

// Sets Model For Another JTable(jtblLGP) In My Form Get Data From DB
getLotGradePriceData();
  • not good idea update Swing GUI this way,你是块EDT,直到JDBC事件结束,

  • 使用Runnable#Thread(所有输出到Swing GUI,其XxxModel必须包装到invokeLater)或SwingWorker作为此职位的工作线程,

    < / LI>
  • 使用ListSelectioListener作为最简单的方法

  • 也许还有一些问题,为了更好地帮助发布SSCCE,简短,可运行,可编译,使用局部变量而不是JDBC事件