[Solved] Is there an “onChange” for Java? [closed]


There is no generic onChange function. However there is a method you can define in a class that implements KeyListener which is public void keyPress. You would use this something like the following:

public class MyClass implements KeyListener {

    private JTextField myField;
    private JLabel myLabel;

    public MyClass() {
        myLabel = new JLabel("Enter text here");
        myField = new JTextField();
        myField.addKeyListener(this);
    }

    @Override
    public void keyPress(KeyEvent e) {
        myLabel.setText("");
    }
}

There is of course a lot more flexibility you can add to this, but the above is the general idea. For example, you could make sure that the KeyEvent is coming from the appropriate source, as rightly you should:

@Override
public void keyPress(KeyEvent e) {
    if(e.getSource() == myField) {
        myLabel.setText(""):
    }
}

… and lots of other stuff. Have a look at Oracle’s Event Listener tutorials.

2

solved Is there an “onChange” for Java? [closed]