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   string9527  阅读(1517)  评论(0)    收藏  举报

努力加载评论中...

导航

点击右上角即可分享
微信分享提示