0

I made a submit button but it will only be set enabled if there is text in the field. And the submit button will disable if there's no text in the field. However when I enter a, the submit button doesn't become enable and it requires me entering another character for it to do so. And I have to press the delete button again even thought I deleted all the character in the box for the button to disable.

I don't understand why this is happening, I put a print statement to see what happens as I enter characters. I noticed it prints out what I entered before the last key press.

public void keyPressed(KeyEvent e) {
    System.out.println(inputField.getText()); // test input
    if(inputField.getText().trim().length() == 0) 
        submit.setEnabled(false);

    if(e.getKeyCode() == KeyEvent.VK_ENTER) {
        if(inputField.getText().trim().length() == 0)
             JOptionPane.showMessageDialog(this, "Invalid input.", "Error", JOptionPane.ERROR_MESSAGE);
        else
            displayMessage();
        return;
    } 

    if(inputField.getText().trim().length() > 0) {
        submit.setEnabled(true);
    }
}
tenkii
  • 449
  • 2
  • 10
  • 23
  • you can use a [value change listener](http://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield) instead.. – chancea Apr 28 '15 at 18:32
  • 1
    Use `keyReleased` instead of `keyPressed` and `keyTyped`. Looks like the event triggers when the key is pressed, but before the field is updated. With `keyTyped`, it works for deleting characters that are currently in the field, but you would still need to enter 2 characters to get your button to work again. http://pastebin.com/G7KW1B9p – Clark Kent Apr 28 '15 at 18:44

2 Answers2

1

Use keyReleased instead of keyPressed and keyTyped. Looks like the event triggers when the key is pressed, but before the field is updated. With keyTyped, it works for deleting characters that are currently in the field, but you would still need to enter 2 characters to get your button to work again.

import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class TextField extends JFrame {

        JTextArea inputField;
        JButton    submit;

        public TextField()
        {
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                inputField = new JTextArea("hi");
                submit = new JButton("Submit");
                inputField.addKeyListener(new KeyListener() {

                        @Override
                        public void keyPressed(KeyEvent arg0) {
                                //func(arg0);
                        }

                        @Override
                        public void keyReleased(KeyEvent arg0) {
                                func(arg0);
                        }

                        @Override
                        public void keyTyped(KeyEvent arg0) {
                                //func(arg0);
                        }
                });
                this.setLayout(new GridLayout(0,1));
                this.add(inputField);
                this.add(submit);
                this.pack();
                this.setVisible(true);
        }

        void func(KeyEvent arg0)
        {
            System.out.println(inputField.getText() + "length: " + inputField.getText().trim().length()); // test input
            if(inputField.getText().trim().length() == 0)
                submit.setEnabled(false);

            if(arg0.getKeyCode() == KeyEvent.VK_ENTER) {
                if(inputField.getText().trim().length() == 0)
                        System.out.println("error");
                else
                    System.out.println("success");
                return;
            }
            if(inputField.getText().trim().length() > 0) {
                submit.setEnabled(true);
            }
        }

        public static void main(String [] args)
        {
                new TextField();
        }
}
Clark Kent
  • 1,178
  • 1
  • 12
  • 26
0

Use keyTyped instead of keyPressed.

Magnilex
  • 11,584
  • 9
  • 62
  • 84