engz
engz

Reputation: 45

JTextField only allowing one letter at once?

I'm trying to make an application where the user hits the button/or hits enter and the input goes to the JList. I have successfully created the application, but I've ran into one small problem while clearing the JTextField of the input. It only allows me to enter one symbol at a time. It will work if I take this out of the ActionListener and KeyListener:

textField.setText("");

Is there a way to make it allow the user to enter how many symbols as they want? The error parts of my code are:

final JButton btnAdd = new JButton("ADD");
    btnAdd.setToolTipText("Add the item to the list");
    btnAdd.setBounds(157, 98, 68, 23);
    contentPane.add(btnAdd);
    btnAdd.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae) {
            v.add(textField.getText());
            list.setListData(v);
            textField.setText("");
        }
    });

    textField = new JTextField();
    textField.setToolTipText("Enter the item here");
    textField.setBounds(10, 99, 137, 20);
    contentPane.add(textField);
    textField.setColumns(10);
    textField.addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent ke){
            btnAdd.setEnabled(true);

            if(ke.getKeyCode()==KeyEvent.VK_ENTER)
                btnAdd.doClick();
                textField.setText("");
        }
    });

Upvotes: 2

Views: 1170

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347184

Don't use a KeyListener on text components, you can end up with a mutation excepts, key strokes can be consumed by the text component, never reaching your listener and doesn't take into account what happens when the user pastes content into the field.

Based on your example, you should simply be using an ActionListener, in fact, you can use the same one you added to your button

ActionListener listener = new ActionListener(){
    public void actionPerformed(ActionEvent ae) {
        v.add(textField.getText());
        list.setListData(v);
        textField.setText("");
    }
};

btnAdd.addActionListener(listener)(

textField = new JTextField();
textField.addActionListener(listener);

Take a look at How to use text fields for more details

Don't use setSize, setLocation or setBounds on components. You should rely on an appropriate layout manager. Modern UIs are expected to run on different platforms with different screen resolutions, dpi, font metrics and rendering pipelines all of which effects the size of text on the screen...

Upvotes: 1

Jason Nichols
Jason Nichols

Reputation: 11733

Simple answer: put braces around your if statement! You've got this:

if(ke.getKeyCode()==KeyEvent.VK_ENTER)
  btnAdd.doClick();
  textField.setText("");

Which is the equivalent of this:

if(ke.getKeyCode()==KeyEvent.VK_ENTER) {
  btnAdd.doClick();
}

textField.setText("");

You should use braces for all if statements, no matter how small:

if(ke.getKeyCode()==KeyEvent.VK_ENTER) {
  btnAdd.doClick();
  textField.setText("");
}

What you were doing was causing the textField.setText() to happen on every key press.

Upvotes: 1

Solace
Solace

Reputation: 2189

public void keyPressed(KeyEvent ke){
        btnAdd.setEnabled(true);

        if(ke.getKeyCode()==KeyEvent.VK_ENTER)
            btnAdd.doClick();
            textField.setText("");
}

Your textField.setText(""); is OUTSIDE of the if statement. Therefore, your text field will clear every time the user presses a key.

Should be:

public void keyPressed(KeyEvent ke){
        btnAdd.setEnabled(true);

        if(ke.getKeyCode()==KeyEvent.VK_ENTER){
            btnAdd.doClick();
            textField.setText("");
        }
}

Upvotes: 2

Related Questions