Java中GUI——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值。

Java中GUI——JTable中鼠标监听的添加