Simultaneously update text area while typing in text field

前端 未结 4 453
无人及你
无人及你 2021-01-14 03:04

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

相关标签:
4条回答
  • 2021-01-14 03:39

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

    0 讨论(0)
  • 2021-01-14 03:51

    You should do that under the keyReleased event instead of the keyTyped and it will work as you need.

    0 讨论(0)
  • 2021-01-14 03:52

    I would not recommend using KeyListeners

    Simply add a DocumentListener to your JTextField via:

    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,removeUpdate and changedUpdate) simply put in a call to set the text of your JTextArea via 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);
        }
    }
    
    0 讨论(0)
  • 2021-01-14 03:53

    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.

    0 讨论(0)
提交回复
热议问题