我正在尝试使用默认组件为可编辑的 JTable
实现撤消(和重做)功能。 JTable
有一个额外的类来指定其属性,称为 SpecifiedJTable
。
为此,我想捕获双击单元格的时刻(即选择/标记要编辑的单元格的时刻),将单元格中的信息及其坐标推送到堆栈上。
这应该由 MouseListener
完成......至少这是我的想法。
我尝试了这个(站在我的 SpecifiedJTable
类的构造函数中)
class JTableSpecified extends JTable {
private static final long serialVersionUID = 1L;
private int c; // the currently selected column
private int r; // the currently selected row
public JTableSpecified(String[][] obj, String[] columnNames) {
super(obj, columnNames); // constructs the real table
// makes that you can only select one row at a time
this.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
// makes that columns are not squeezed
this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// forbids to rearrange the columns
getTableHeader().setReorderingAllowed(false);
// adds action listener
this.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
r = getSelectedRow();
c = getSelectedColumn();
// get the String at row r and column c
String s = (String) getValueAt(r, c);
if (jobDisplayed) jobSwitch(c, s);
else resSwitch(c, s);
}
});
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println("test");
}
}
});
}
}
但不知何故,clickCounter 不想达到任何高于 1 的值。
我很高兴得到任何答复和帮助。谢谢。
最佳答案
您遇到的问题与使用 mouseClicked()
而不是使用 mousePressed()
有关。在这种情况下,增加点击计数器似乎非常困难,但仍然是可能的。我花了很多时间点击和移动鼠标才将点击计数器增加到 1 以上。您可以在代码中自己尝试一下。为了让计数器超过 1,你需要通过快速按下和释放来疯狂地使用鼠标,同时将鼠标从一个单元格移动到另一个单元格(或者也许我只是幸运地在单元格之间单击?)。
正如您在这个由您的代码编写的完整工作示例中所看到的,使用 mousePressed()
方法可以很好地检测到两次鼠标按下。
public class JTableSpecified extends JTable {
private static final long serialVersionUID = 1L;
public JTableSpecified(String[][] obj, String[] columnNames) {
super(obj, columnNames); // constructs the real table
// makes that you can only select one row at a time
this.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
// makes that columns are not squeezed
this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// forbids to rearrange the columns
getTableHeader().setReorderingAllowed(false);
// adds action listener
this.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
}
});
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println("test");
}
System.out.println("e.getClickCount() = " + e.getClickCount());
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JPanel panel = new JPanel();
panel.add(new JTableSpecified(new String[][]{{"oi", "oi2"}, {"oi3", "oi4"}}, new String[]{"Col1", "Col2"}));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(panel);
f.pack();
f.setVisible(true);
}
});
}
}
结论:也许您实际上想使用 mousePressed()
方法?
关于java - JTable 不会监听 Doubleclicks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105133/