Devi aggiungere un actionListener ad ogni bottone che crei, io farei così:
codice:
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyButton extends JPanel{
private JButton pulsante;
public MyButton(String testo) {
setLayout( new GridLayout(1, 1) );
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
pulsante = new JButton(testo);
pulsante.addActionListener(new MyListener());
add(pulsante);
}
private class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
myTextField.setText(e.getSource().getText());
}
}
}