JTextField textField = new JTextField(12);
textField.addActionListener(new ActionListener() {
double factor = 0.12;
public void actionPerformed(ActionEvent e) {
// User has pressed return/enter to get here:
JTextField textField = (JTextField)e.getSource();
String text = textField.getText();
// Take care that text is parsable...
double d = Double.parseDouble(text);
d *= factor;
textField.setText(String.valueOf(d));
}
});
// Add textField to your gui...
// Or, test with something like:
JOptionPane.showMessageDialog(null, textField, "", -1);
// Pretend the user has typed in the value "25"
// Programmatically we could do it with:
textField.setText(25);
// User presses the "return/enter" key while
// textField has the focus (blinking caret)
// which generates an ActionEvent which is
// reported to all listeners registered to
// recieve ActionEvents from/on this textField.