Reputation: 1
I am making an applet in which people have to enter a 4-number pincode so that they can withdraw a sum of money. However, the withdraw button is deactivated by default, and is only activated when a single code is entered in the pin textfield. What would be the best way to do this?
Upvotes: 0
Views: 410
Reputation: 5525
DJClayworth is right on the money.
If you need a concrete example using pure AWT, here it is:
package com.example;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Example extends Frame {
private static final long serialVersionUID = 1L;
private final Button button;
public Example() {
super("Example");
addWindowListener(new WindowAdapter() { //exit when user closes the frame
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
TextField textField = new TextField(4);
textField.addTextListener(new TextListener() {
@Override
public void textValueChanged(TextEvent e) {
boolean enableButton = textField.getText().length() == 4;
button.setEnabled(enableButton);
}
});
button = new Button("Submit");
button.setEnabled(false);
Panel panel = new Panel();
add(panel);
panel.add(textField);
panel.add(button);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Example();
}
}
Upvotes: 0
Reputation: 26886
You need to create a Listener and attach it to the TextField. The Listener will fire e very time the contents of the TextField changes. Have the Listener test the contents of the TextField, and if it is correct activate the Button.
Documentation of TextField should give you everything you need to know.
Upvotes: 1