简体   繁体   中英

How can i add an UndoableEditListener to JTable cells?

Can i add an UndoableEditListener to a JTable? For example with a JTextField we do this :

textField = new JTextField();
      Document doc = textField.getDocument();
      doc.addUndoableEditListener(new MyUndoableEditListener());

You can do that for CellEditor of the JTabel in next way:

use DefaultCellEditor with JTextField :

JTextField field = new JTextField();
field.getDocument().addUndoableEditListener(new UndoableEditListener() {

    @Override
    public void undoableEditHappened(UndoableEditEvent arg0) {
        System.out.println("profit");
    }
});
DefaultCellEditor editor = new DefaultCellEditor(field);
table.getColumnModel().getColumn(COLUMN_INDEX).setCellEditor(editor);

table is your JTable and COLUMN_INDEX index of needed column.

No one stop use to assign this listener to the Document of of Text Field cell editor of JTable :) For your table use a custom cell editor, implement the UndoableEditListener to the editor's text component's Document.

Check out the Official Tutorial page for example and demo using CellEditor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM