java 在文本字段中输入时同时更新文本区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12677638/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:48:29  来源:igfitidea点击:

Simultaneously update text area while typing in text field

javaswingjtextfieldjtextarea

提问by harry4

I want to update my text area along with typing in the text field but i get a delay of 1 keystroke while typing i.e when i press a key the previous key is displayed.Here is my snippet

我想在输入文本字段的同时更新我的​​文本区域,但我在输入时延迟了 1 次击键,即当我按下一个键时会显示前一个键。这是我的代码片段

private void jTextField1KeyTyped(java.awt.event.KeyEvent evt)
{         
    String a = jTextField1.getText();
    jTextArea1.setText(a);            
}  

回答by David Kroukamp

I would not recommend using KeyListeners

我不建议使用 KeyListeners

Simply add a DocumentListenerto your JTextFieldvia:

只需将一个添加DocumentListener到您的JTextField通过:

textField.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent de) {
            }

            @Override
            public void removeUpdate(DocumentEvent de) {
            }

            @Override
            public void changedUpdate(DocumentEvent de) {
            }
        });

Inside each of the methods ( insertUpdate,removeUpdateand changedUpdate) simply put in a call to set the text of your JTextAreavia setText():

在每个方法 ( insertUpdate,removeUpdatechangedUpdate) 中,只需调用即可设置JTextAreavia的文本setText()

textArea.setText(textField.getText());

Here is an example I made:

这是我做的一个例子:

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        final JFrame frame = new JFrame("Test");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComponents(frame);

        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(JFrame frame) {
        final JTextField jtf = new JTextField(20);
        final JTextArea ta = new JTextArea(20,20);

        ta.setEditable(false);

        jtf.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent de) {
                ta.setText(jtf.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent de) {
                ta.setText(jtf.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent de) {
            //Plain text components don't fire these events.
            }
        });

        frame.getContentPane().add(jtf, BorderLayout.WEST);
        frame.getContentPane().add(ta, BorderLayout.EAST);
    }
}

回答by Dan D.

You should do that under the keyReleasedevent instead of the keyTypedand it will work as you need.

您应该在keyReleasedevent 而不是 the下执行此操作keyTyped,它将根据您的需要工作。

回答by Jonathan Drapeau

You need to wait till the event on your TextField is processed before updating the TextArea. Your code update the TextArea before the TextField is done processing the new typed character. Hence the text set in the TextArea is one keystroke behind.

在更新 TextArea 之前,您需要等到处理 TextField 上的事件。您的代码在 TextField 完成处理新键入的字符之前更新 TextArea。因此,TextArea 中设置的文本落后于一个按键。

回答by AqueousSnake

You could try using recursion by referencing the method inside the method (avoid loops though).

您可以通过引用方法内部的方法来尝试使用递归(尽管避免循环)。