我正在尝试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 的值。
我很高兴得到任何答案和帮助。谢谢。