Java Swing:如何获取包括刚刚输入的字符在内的 TextArea 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2137111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing: How to get TextArea value including the char just typed?
提问by Ondra ?i?ka
What's the best way to get a value of a TextArea after a key is typed, including this character?
键入键(包括此字符)后获取 TextArea 值的最佳方法是什么?
If I do it in the even listener, textarea.getText()returns the value without the eventual new char.
如果我在偶数侦听器中执行此操作,则textarea.getText()返回没有最终新字符的值。
Basically I see two ways:
基本上我看到两种方式:
postponing processing with something like invokeLater(). I would prefer a solution without threads.
figuring out where to put the char into the text, based on the carret position.
使用诸如 invokeLater() 之类的方法推迟处理。我更喜欢没有线程的解决方案。
根据插入符号的位置找出将字符放入文本的位置。
Is there any other, simpler?
还有其他更简单的吗?
Thanks.
谢谢。
Edit:This is what I have:
编辑:这就是我所拥有的:
JTextArea textarea = (JTextArea) evt.getComponent();
String texySource = textarea.getText();
char keyCode = evt.getKeyChar();
//if( Character.isLetterOrDigit( keyCode ) || Character.isSpaceChar( keyCode ) )
if( keyCode >= 0x20 || keyCode == 0x0A || keyCode == 0x0D ){
// TODO: The carret doesn't have to be at the end...
//texySource += Character.toString( evt.getKeyChar() );
String ch = Character.toString( evt.getKeyChar() );
texySource = StringUtils.overlay(texySource, ch,
textarea.getSelectionStart(),
textarea.getSelectionStart() );
}
回答by shemnon
Have you considered a document listener? possibly armed by the typing event?
您是否考虑过文档侦听器?可能由打字事件武装?
class TheListener implements DocumentListener, KeyListener {
boolean armed;
void keyPressed(KeyEvent ignore) { }
void keyReleased(KeyEvent ignore) { }
void keyTyped(KeyEvent e) {
armed = true;
SwingUtilities.invokeLater(new Runnable() { public void run() {
armed = false;
}
}
void deleteUpdate(DocumentEvent e) {
changeUpdate(e);
}
void insertUpdate(DocumentEvent e) {
changeUpdate(e);
}
void changedUpdate(DocumentEvent e) {
if (armed) {
String s = ((JTextComponent)e.getSource()).getText();
//.... whatever you want to do now
}
}
}
//...
TheListener aListener = new TheListener();
textArea.addKeyListener(aListener);
textArea.getDocument().addDocumentListener(aListener);
The theory is to arm the document change listener on a key typed, then add an EDT event to disarm it. The document changes will occur first before disarmed. Once armed, you can assume that any document changes were caused in some part by the key typing event. (warning, I haven't compiled this code, YMMV).
理论是在键入的键上武装文档更改侦听器,然后添加一个 EDT 事件来解除它。文件更改将在解除之前首先发生。准备好后,您可以假设任何文档更改都是由按键输入事件引起的。(警告,我还没有编译这个代码,YMMV)。
回答by Suraj Chandran
You need to use a DocumentListenerand wirte your code in one of the xxxupdate() methods.
您需要使用DocumentListener并在 xxxupdate() 方法之一中编写代码。
回答by fasseg
Have you tried registering a KeyListener with a custom implementation of keyReleased(KeyEvent e) ?
您是否尝试过使用 keyReleased(KeyEvent e) 的自定义实现注册 KeyListener ?
check the api here: KeyListener
在此处检查 api:KeyListener
sun's tutorial with examples: How to write a Key Listener
sun 的示例教程:如何编写 Key Listener

