如何在javafx中将TextArea的isUndoable属性设置为true?

时间:2020-02-02 09:14:00

标签: java javafx

我正在尝试使用JavaFx创建一个简单的文本编辑器,当我按下MenuItem时将textArea.undo()作为一个偶数处理程序时,它将不起作用!撤消功能仅在按Ctrl + Z时有效,因此我将其签出,并注意到默认的isUndoable属性设置为false!那我该如何改变呢?

这是我的代码:

MenuItem undo = new MenuItem("Undo");
undo.setAccelerator(new KeyCodeCombination(KeyCode.Z, KeyCombination.CONTROL_DOWN));
undo.setOnAction(e -> textArea.undo());

我还尝试了以下代码:

textArea.isUndoable() = true;

但是事实证明我做不到!

1 个答案:

答案 0 :(得分:2)

无法设置此属性 ,因为它取决于内部编辑历史记录。您不能直接修改此列表。在您的情况下,历史记录恰好没有更多可撤销的状态

您所能做的就是尝试使历史重复使用replaceText(或其他使用它的编辑方法),使其看起来像您想要的样子,但是即使分组更改,也不能完全控制编辑历史记录。

String wrongText = "Hello Worlt";
textArea.insertText(0, wrongText);
textArea.deleteText(wrongText.length()-1, wrongText.length());
textArea.insertText(wrongText.length()-1, "d");
// now there should be 2 changes to undo
相关问题