table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent arg0) {
int row = table.getSelectedRow(); // 获得当前选中的行号
ID = table.getValueAt(row, 0);
System.out.println(ID);
}
}); 这是在表格模型中做的单击表格任意行时,左键事件,现在要求在这个基础上,实现右键事件,如何实现?

解决方案 »

  1.   

    public void mouseClicked(final MouseEvent arg0) {
     if (SwingUtilities.isRightMouseButton(e)) {
    int row = table.getSelectedRow(); // 获得当前选中的行号
    ID = table.getValueAt(row, 0);
    System.out.println(ID);
     }
      

  2.   

    首先需要给你的表格添加一个鼠标点击事件,然后在事件中做判断,判断是否是右键
    例如:
    public void tableRecord_mouseClicked(MouseEvent e) {
            // 3 代表右键
            if (e.getButton() == 3) {
                //这里写你需要实现的功能,例如:
                  JOptionPane.showMessageDialog(this, "我是右键!");
            }
        }
      

  3.   

    public void mouseClicked(final MouseEvent arg0) { 
     if (SwingUtilities.isRightMouseButton(e)) { 
    int row = table.getSelectedRow(); // 获得当前选中的行号 
    ID = table.getValueAt(row, 0); 
    System.out.println(ID); 
     } 
      

  4.   

    还是不行,提示e cannot be resolved!
      

  5.   

    public void tableRecord_mouseClicked(MouseEvent e) { 
            // MouseEvent.Button3 代表右键 
            if (e.getButton() == MouseEvent.Button3 ) { 
                //这里写你需要实现的功能,例如: 
                  JOptionPane.showMessageDialog(this, e.getX(),e.getY()); 
                //this需替换成你要在哪个组件上显示的名称
                 //e.getX(),e.getY()分别为横坐标和纵坐标
            } 
        }