Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java TextField getText() returns previous string value

I have a problem with Java Textfield that is when I cover all text in the JTextField and input new text immediately(do not pass backspace) into the JTextField, then I use function getText() I get the previous string not current string. Please help for some solutions. Thanks in advance.

like image 908
PlodPla Avatar asked Jul 06 '11 03:07

PlodPla


People also ask

What does the getText () method returns?

GetText returns the text from the single-line text field. It returns only the first line of a multi-line text field.

What is the purpose of setText () and getText () methods?

The getText() method of java. text. BreakIterator class is used to get the text previously set by the setText() method in breakiterator.

Which method of JTextField returns the selected text contained by the text field?

Notice the use of JTextField 's getText method to retrieve the text currently contained by the text field. The text returned by this method does not include a newline character for the Enter key that fired the action event.

What is TextField setColumns ()?

setColumns(int n) :set the number of columns of the text field. setFont(Font f) : set the font of text displayed in text field.


3 Answers

I just tested the problem you described by adding a keyListener to a JTextField and printing the getText() method's return value to the console.

What I found out is that it is always one character behind if you want to use the getText() method right in the keyTyped or keyPressed event (I didn't know this because I usually just use a button to confirm I'm done entering the text and bind a KeyEvent to the Return key to trigger the button if a user wants to confirm by hitting enter)

I think this is due to the textField updating its text value AFTER the event is shot.

I assume this is what you did since you didn't provide sample code, so I'll delete this answer if it's not.

The work around to this is to implement what you want to do in the keyReleased method instead.

public void keyReleased(Event e)
{
  System.out.println(myTextField.getText());
}
like image 81
Adam Smith Avatar answered Nov 03 '22 18:11

Adam Smith


Don't use a KeyListener. The character has NOT been added to the Document when the keyPressed() event is fired.

Add an ActionListener to a JButton. This way the user clicks on the button when text is finised being entered.

Also, in the future post a SSCCE with you question so we can better understand what you are trying to do.

like image 40
camickr Avatar answered Nov 03 '22 20:11

camickr


for example :

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

public class TextLabelMirror {

    private JPanel mainPanel = new JPanel();
    private JTextField field = new JTextField(20);
    private JTextField field1 = new JTextField(20);

    public TextLabelMirror() {
        field.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            private void updateLabel(DocumentEvent e) {
                java.awt.EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        field1.setText(field.getText());
                    }
                });
            }
        });

        mainPanel.setLayout(new GridLayout(1, 0, 10, 0));
        mainPanel.add(field);
        mainPanel.add(field1);
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("TextLabelMirror");
        frame.getContentPane().add(new TextLabelMirror().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}
like image 2
mKorbel Avatar answered Nov 03 '22 20:11

mKorbel