我已编写此java代码来检测鼠标左键双击,但此代码无效请求帮助。
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class B extends MouseAdapter {
JFrame frame = new JFrame();
Object rows[][] = new Object[5][3];
String colums[] = {"A","B","C"};
JTable table = new JTable(rows,colums);
JScrollPane scroll = new JScrollPane(table);
public static void main(String arg[]) {
new B();
}
B() {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
table.addMouseListener(this);
frame.add(scroll);
frame.setVisible(true);
}
public void mouseClicked(MouseEvent clicked) {
if(clicked.getSource()==table && clicked.getButton()==1 && clicked.getClickCount()==2)
System.out.println("Left Double Click");
}
}
答案 0 :(得分:0)
SwingUtilities.isLeftMouseButton(clicked)
代替clicked.getButton()==1
MouseEvent
之前,该表可能正在使用MouseListener
并安装单元格编辑器。table.setFillsViewportHeight(true);
,则可以在行/列外成功双击您可以更改表格的CellEditor
以忽略MouseEvent
(或更改所需的点击次数),这样您MouseListener
可以获得双击,但是也会增加您的工作量,因为您需要为每列CellEditor
类型提供Class
TableCellEditor editor = new DefaultCellEditor(new JTextField(10)) {
@Override
public boolean isCellEditable(EventObject anEvent) {
boolean editable = false;
if (!(anEvent instanceof MouseEvent)) {
editable = super.isCellEditable(anEvent);
}
return editable;
}
};
table.setDefaultEditor(Object.class, editor);
没有更多的背景,很难知道还有什么建议