0

I added a KeyListener to a text input, and it doesn't seem to register the fact I am pressing enter, because it doesn't even print out the text values. Any ideas?

        txtPort.addKeyListener(new KeyAdapter() {
            public void KeyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    System.out.println(txtName.getText());
                    System.out.println(txtAddress.getText());
                    System.out.println(txtPort.getText());
                    if(txtName.getText().equals("") && txtAddress.getText().equals("") && txtPort.getText().equals("")) {
                        String name = txtName.getText();
                        String address = txtAddress.getText();
                        int port = Integer.parseInt(txtPort.getText());

                        login(name, address, port);
                    }
                }
            }   
    }); 
  • 1
    `lblPort` sounds like something you'd name a label, not a textbox. I guess you've connected the listener to the wrong control. Especially since you call the textbox `txtPort` a bit later on. – TessellatingHeckler Nov 26 '15 at 20:27
  • I guess you want to use `&&` (Logical AND) and not `&` (Bitwite AND). Also could you provide a [Runnable Example](http://stackoverflow.com/help/mcve)? Probably you added the key listener to a `JLabel` instead of a `JTextField`... – Frakcool Nov 26 '15 at 20:27
  • 1
    1) what is a "text input"? or "textbox"? 2) if you're talking about a JTextComponent such as a JTextField, JTextArea, JTextPane or JEditorPane, then the best solution is **not** to use a KeyListener but rather a DocumentFilter or DocumentListener, depending on your needs. – Hovercraft Full Of Eels Nov 26 '15 at 20:29
  • 2
    This is unrelated, but I noticed you are comparing strings wrong. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java do not use **!=** on String – WalterM Nov 26 '15 at 20:30
  • 1
    More on what @WalterM states above, please note that the reason that you don't want to compare Strings using `==` or `!=`, and instead want to use the `equals(...)` or the `equalsIgnoreCase(...)` method instead is because `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Nov 26 '15 at 20:39

3 Answers3

1

Don't use a KeyListener.

Instead just add an ActionListener to the JTextField. The ActionListener will be invoked when the Enter key is pressed.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • But don't I need the KeyEvent? Having the ActionListener doesn't let me use the KeyEvent e part – Josh Marchand Nov 26 '15 at 21:12
  • @JoshMarchand, You don't need to check the KeyEvent. This is a simpler API to use. The ActonListener will only be invoked when the "Enter" key is pressed. Rarely do you need to use a KeyListner. Swing has better API's to use. – camickr Nov 26 '15 at 22:45
0

You are using the & (bitwise AND) operator.
See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

You should use the && (logical AND) operator.
See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

And to check if a string is filled in, use the isEmpty method.

if (((txtName.getText() != null) && !txtName.getText().isEmpty()) &&
    ((txtAddress.getText() != null) && !txtAddress.getText().isEmpty()) &&
    ((txtPort.getText() != null) && !txtPort.getText().isEmpty()))
  • That doesn't address the question part "*[..] it doesn't even print out the text values.*" - the value printing should happen before the 'and' testing. – TessellatingHeckler Nov 26 '15 at 20:44
0

Just submitting my comment as a possible answer. Change the first line:

lblPort.addKeyListener(new KeyAdapter() {

to

txtPort.addKeyListener(new KeyAdapter() {

I think you connected the listener to the wrong control, and it never fires. You call txtPort.GetText() later, so that seems a likely correct name for the textbox control.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87