撤消/重做Jtextarea
我正在写文本编辑器。在这里,我遇到了一个问题。我需要为JTEXTAREA实现撤消和重做功能。为此,我使用Undomanager。但是,如果我取消或返回,则该操作将一次取消或返回一个字符。如何做到这一点,以免动作取消或返回,而不是通过字符而不是通过言语返回。
这就是我的代码的样子:
jta.getDocument().addUndoableEditListener( new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
}
});
public static void undo() {
if (undoManager.canUndo())
try {
undoManager.undo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
}
public static void redo() {
if (undoManager.canRedo())
try {
undoManager.redo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
}
不幸的是,该解决方案不起作用。我不明白如何使用它,因为我没有足够的编程经验: jtextpane undo and redo整个单词
I am writing a text editor. And here I ran into a problem. I need to implement UNDO and Redo function for JTextArea. For this I use UndoManager. But, if I cancel or return, then the actions will be canceled or returned one character at a time. How to make it so that actions are canceled or returned not by character, but by word.
This is what my code looks like:
jta.getDocument().addUndoableEditListener( new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
}
});
public static void undo() {
if (undoManager.canUndo())
try {
undoManager.undo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
}
public static void redo() {
if (undoManager.canRedo())
try {
undoManager.redo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
}
Unfortunately, this solution doesn't work. I don't understand how to use it as I don't have enough programming experience:
JTextPane undo and redo whole words
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了一些谷歌搜索,然后遇到 jtextpane> >,这引导我到合并一个无效的人在Jeditorpane中合并在一起。
我将其拉开并开始玩耍的概念,这使我遇到(至少一个)问题 - 如果您选择所有文本并删除它,则会导致
badlocationException
。因此,我对此有更多的玩法,我相信我已经为此实施了合适的修复程序。我还添加了
ChangElistener
支持,因此您可以在状态更改时获得通知。可运行的示例
I did a little bit of googling and I came across JTextPane undo and redo whole words, which lead me to Merging UndoableEdits in one to be undone all together in JEditorPane.
Intrigued by the concept, I pulled it apart and started playing around with, which lead me to (at least one) issue - if you select all the text and delete it, it causes a
BadLocationException
. So I had a bit more of play around with it and I believe I've implemented a suitable fix for it.I also added
ChangeListener
support, so you can get notified when the state changes.Runnable Example