If my question wasn\'t very specific, here is what I am trying to do. I have a calculator that has two JTextFields, a JLabel (\"Answer = \"), and a JTextField for the answer.
Instead of using an ActionListener you can add an Action to the button. In this case you will want to extend TextAction because it has a method that allows you to get the last focused text component so you can insert the digit into that component. The code would be something like:
class AddDigit extends TextAction
{
private String digit;
public AddDigit(String digit)
{
super( digit );
this.digit = digit;
}
public void actionPerformed(ActionEvent e)
{
JTextComponent component = getFocusedComponent();
component.replaceSelection( digit );
}
}