Вопрос

I have 2 textfields in my project. The first textfield is txtNumA and the second is txtNumB. I disabled txtNumB. When txtNumA is not empty, txtNumB will be enabled.

Well, this is part of code I've tried:

private void txtNumKeyTyped(java.awt.event.KeyEvent evt) {                                   
    if(!(txtNumA.getText().trim().equals(""))){  
        txtNumB.setEnabled(true); 
    }
    else { 
        txtNumB.setText(null);
        txtNumB.setEnabled(false);
    } 
}

Actually it works, but not perfect. It works only if I typed 2 or more characters in txtNumA. What I need is when I typed one character and more, txtNumB will be enabled.

What's wrong with my code?

Это было полезно?

Решение

What is happening here is,

In case of KeyTyped and KeyPressed events the input is not still given to the TextField.That's why it is not working and works after you type the second character and by that time first character must have reached the TextField.So use KeyReleased method to handle this case.

t is the first TextField and t1 is second.

t.addKeyListener(new KeyListener(){

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyPressed(KeyEvent e) {

        }

        @Override
        public void keyReleased(KeyEvent e) {
            JTextField bt = (JTextField)e.getSource();
            if(bt.getText().trim().length()>0){
                t1.setEnabled(true);
            }
            else
                t1.setEnabled(false);
        }
    });

Другие советы

The correct way is to add a DocumentListener to the Document of your JTextField:

public final class TextFieldListener implements DocumentListener {
    public static void main(final String... args) {
        SwingUtilities.invokeLater(() -> new TextFieldListener().go());
    }

    private final JFrame frame = new JFrame();
    private final JTextField field = new JTextField();
    private final JTextField field2 = new JTextField();

    private TextFieldListener() {
        field.getDocument().addDocumentListener(this);
        frame.setLayout(new GridLayout(2, 0));
        frame.add(field);
        frame.add(field2);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        onFieldUpdated();
    }

    private void go() {
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void onFieldUpdated() {
        setField2Enabled(!field.getText().isEmpty());
    }

    private void setField2Enabled(final boolean enabled) {
        field2.setEnabled(enabled);
    }

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

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

    @Override
    public void changedUpdate(final DocumentEvent e) {}
}

It is not correct to add a KeyListener to your text field if you are interested in changes to its content.

Further reading:

I think the problem with this working for 2 characters is because getText() method returns not updated value, i.e. it returns the value BEFORE the change. What you need to do is somehow update that value before you compare it to empty string.

You may need to investigate KeyEvent to see if user adds another character or is it e.g. backspace...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top