当前位置: 技术问答>java相关
JTABLE的难题:我想在鼠标右键点击JTABLE时被点中的那一行被选中,如何实现?
来源: 互联网 发布时间:2015-06-26
本文导语: 就是要求鼠标左键和右键点击JTABLE的效果一样. | class MyListener extends MouseAdapter{/** public void mousePressed(MouseEvent me){ JTable table=(JTable)me.getSource(); if((me.getModifiers()&InputE...
就是要求鼠标左键和右键点击JTABLE的效果一样.
|
class MyListener extends MouseAdapter{/**
public void mousePressed(MouseEvent me){
JTable table=(JTable)me.getSource();
if((me.getModifiers()&InputEvent.BUTTON3_MASK)!=0){
Point p = me.getPoint();
int row = table.rowAtPoint(p);
int column = table.columnAtPoint(p);
if(!me.isControlDown()&!me.isShiftDown()&row!=-1&column!=-1) table.changeSelection(row, column, me.isControlDown(), me.isShiftDown());
}
System.out.println("confirm");
}
/**
* @param me MouseEvent
*/
public void mouseReleased(MouseEvent me){
if((me.getModifiers()&InputEvent.BUTTON3_MASK)!=0) //show something
}
JTable1.addMouseListener(new MyListener());
public void mousePressed(MouseEvent me){
JTable table=(JTable)me.getSource();
if((me.getModifiers()&InputEvent.BUTTON3_MASK)!=0){
Point p = me.getPoint();
int row = table.rowAtPoint(p);
int column = table.columnAtPoint(p);
if(!me.isControlDown()&!me.isShiftDown()&row!=-1&column!=-1) table.changeSelection(row, column, me.isControlDown(), me.isShiftDown());
}
System.out.println("confirm");
}
/**
* @param me MouseEvent
*/
public void mouseReleased(MouseEvent me){
if((me.getModifiers()&InputEvent.BUTTON3_MASK)!=0) //show something
}
JTable1.addMouseListener(new MyListener());
|
JTable1.addMouseListener(new RightClickListener());
class RightClickListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
selectTable(e);
}
public void mouseReleased(MouseEvent e) {
selectTable(e);
}
private void selectTable(MouseEvent e) {
int row = JTable1.rowAtPoint(e.getPoint());
if(row>=0)
JTable1.setRowSelectionInterval(row,row);
}
}
class RightClickListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
selectTable(e);
}
public void mouseReleased(MouseEvent e) {
selectTable(e);
}
private void selectTable(MouseEvent e) {
int row = JTable1.rowAtPoint(e.getPoint());
if(row>=0)
JTable1.setRowSelectionInterval(row,row);
}
}