JTable中添加监听事件(鼠标点击)

//给table加上一个鼠标事件监听器对象
        table.addMouseListener(new Java.awt.event.MouseAdapter(){
             publicvoid mouseClicked(MouseEvent e) {//仅当鼠标单击时响应
                //得到选中的行列的索引值
               int r= table.getSelectedRow();
               int c= table.getSelectedColumn();
               //得到选中的单元格的值,表格中都是字符串
               Object value= table.getValueAt(r, c);
            String info=r+"行"+c+"列值 : "+value.toString();
            javax.swing.JOptionPane.showMessageDialog(null,info);
             }
         }); 

点击table列表里的元素就可以获取所在行列信息,继而获取它的value值。

posted on 2017-07-27 08:48  string9527  阅读(1451)  评论(0编辑  收藏  举报

导航