我是Java和Swing的新手,我正在尝试创建一个JTable,我想在其中实现一个撤销选项。是否有可能将表模型存储在操作的某个部分,之后当我按下撤销按钮或其他内容时,我可以设置保存的表模型,使其像撤消一样执行。
由于
答案 0 :(得分:0)
您可以尝试使用此代码。这取决于您将在以下链接中找到的TableCellListener代码: http://tips4java.wordpress.com/2009/06/07/table-cell-listener/
public class MyMain extends JFrame {
private static final long serialVersionUID = 1L;
DefaultTableModel model;
JTable table;
String col[] = { "Name", "Address", "Phone" };
Action action = new AbstractAction()
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e)
{
TableCellListener tcl = (TableCellListener)e.getSource();
System.out.println("Row : " + tcl.getRow());
System.out.println("Column: " + tcl.getColumn());
System.out.println("Old : " + tcl.getOldValue());
System.out.println("New : " + tcl.getNewValue());
}
};
public static void main(String[] args) throws IOException {
new MyMain().start();
}
public void start() {
model = new DefaultTableModel(col, 50);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int arg0, int arg1) {
return true;
}
};
JScrollPane pane = new JScrollPane(table);
TableCellListener tcl = new TableCellListener(table, action);
add(pane);
setVisible(true);
setSize(500, 400);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}