我想获得通过鼠标在JTable中点击后的数据行中的数据!我为JTable 添加了MouseListener监听器,可是没有达到目地!还有就是怎样让用户一次只能选中一行数据!

解决方案 »

  1.   

    不用侦听鼠标,只需要侦听表格的选中行就可以了。
    this.table = new JTable();
    //只能选中一行
    this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    //注册选择侦听,其中this是实现了ListSelectionListener的对象
    this.table.getSelectionModel().addListSelectionListener(this);
    ……
    public void valueChanged(ListSelectionEvent e) {
        if(e.getValueIsAdjusting()) return;
        if(e.getSource().equals(this.table.getSelectionModel())){
    if(!this.table.getSelectionModel().isSelectionEmpty()){
                //获取选中行号
                int selectRow = this.table.getSelectRow();
                //然后想干嘛干嘛
                ……
             }
        }
    }
      

  2.   

    使用鼠标监听也可以实现的,getSelectRows就可以了得到行号
    不过如果是已经排过序的JTable,需要注意这样得到的行号可能需要转换一下才是model中的行号