【问题标题】:Undo/Redo functionality in javafx spreadsheetjavafx 电子表格中的撤消/重做功能
【发布时间】:2019-01-19 18:58:14
【问题描述】:

我正在研究处理电子表格视图的 javafx 应用程序。我正在从本地存储导入文件并在我的 javafx 应用程序的电子表格视图中显示。我已经实现了几乎所有功能,但撤消和重做功能对我来说似乎非常困难。即使我没有从哪里开始,会有什么操作案例,不知道:( 请用你的经验和知识帮助我。 提前谢谢你...!

【问题讨论】:

  • 您需要的基本上是跟踪更改,每次进行更改时,您将其推送到堆栈中,每次重做都会弹出。然后保存什么以及如何保存,您需要根据您的应用程序定义它。 (问题也太宽泛了)

标签: java javafx spreadsheet scenebuilder


【解决方案1】:

我已经解决了这个问题,这个解决方案背后的逻辑是:-

public class UndoRedo {

    private SpreadsheetCell cell;

    private String oldValue;

    private String newValue;

    public UndoRedo(SpreadsheetCell cell, String oldValue, String newValue) {
        this.cell = cell;
        this.oldValue = oldValue;
        this.newValue = newValue;
    }

    public SpreadsheetCell getCell() {
        return cell;
    }

    public void setCell(SpreadsheetCell cell) {
        this.cell = cell;
    }

    public String getOldValue() {
        return oldValue;
    }

    public void setOldValue(String oldValue) {
        this.oldValue = oldValue;
    }

    public String getNewValue() {
        return newValue;
    }

    public void setNewValue(String newValue) {
        this.newValue = newValue;
    }

}

在电子表格上添加事件以在单元格中的任何更改操作列表中添加对象。

mGridBase.addEventHandler(GridChange.GRID_CHANGE_EVENT, (GridChange e) -> {
            isCellEdited = true;
            SpreadsheetCell cell = mGridBase.getRows().get(e.getRow()).get(e.getColumn());
            String oldValue = lastValue;
            UndoRedo undoRedo = new UndoRedo(cell, oldValue, cell.getText());
            undoRedoList.add(undoRedo);
        }); 

现在在

上添加 Key 事件

Ctrl + Z

用于 UNDO 上次更改

if (KeyCode.Z == event.getCode() && event.isControlDown()) {
                if (!undoRedoList.isEmpty()) {
                    UndoRedo undoRedo = undoRedoList.remove(undoRedoList.size() - 1);
                    undoRedo.getCell().setItem(undoRedo.getOldValue());
                    mSpreadsheet.getSelectionModel().clearAndSelect(undoRedo.getCell().getRow(), mSpreadsheet.getColumns().get(undoRedo.getCell().getColumn()));
                }
            }

现在它的工作完美可靠

Grid grid = ...;  Stack<GridChange> undoStack = ...;  grid.addEventHandler(GridChange.GRID_CHANGE_EVENT, new EventHandler<GridChange>() {

         public void handle(GridChange change) {
                 undoStack.push(change);
             }
         });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    相关资源
    最近更新 更多