Anyone know how to store numbers onto JButtons
? For example every time the user presses a button, I want to record that number.

- 168,117
- 40
- 217
- 433

- 1,033
- 3
- 10
- 10
-
1What do you mean by "*record*" and "*store numbers onto JButtons*"? – mre Nov 02 '11 at 12:40
-
I'm guessing that your end goal is not the retrieval of numbers from JButtons. Your question sounds like you're trying to achieve something in a possibly roundabout way. What are you trying to do in a broader sense, and what are the specifics of this case? where do the numbers need to end up? store and record can both refer to a variety of things. – Dogmatixed Nov 02 '11 at 12:45
-
1For code that can fulfill the entire spec, see [this answer](http://stackoverflow.com/questions/7441625/how-to-find-a-button-source-in-awt-calculator-homework/7441804#7441804). – Andrew Thompson Nov 02 '11 at 13:26
-
Or this related [answer](http://stackoverflow.com/questions/7702697/how-to-get-x-and-y-index-of-element-inside-gridlayout/7706684#7706684). – trashgod Nov 02 '11 at 16:38
4 Answers
You should use ActionListener, and you may use a list, as I show below:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class NumberButtons extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NumberButtons().setVisible(true);
}
});
}
private List<JButton> buttons = new ArrayList<JButton>();
private ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = buttons.indexOf(e.getSource());
System.out.println("Number " + index + " pressed");
}
};
public NumberButtons() {
JPanel pNum = new JPanel();
pNum.setLayout(new GridLayout(3,4));
for (int i = 0; i < 10; ++i) {
JButton b = new JButton("" + i);
b.addActionListener(listener);
pNum.add(b);
buttons.add(b);
}
this.add(pNum);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
}

- 135,866
- 28
- 264
- 277
I assume you have a class, in which there is a button you click. Make this class implement a listener interface. If I recall correctly, the interface is ActionListener
, and the method is actionPerformed
. In this method increase the counter for that particular button. You can store numbers in a HashMap<JButton, Integer>
, for example.

- 7,052
- 2
- 29
- 39
Simply link an ActionListener
to your button and increment a variable every time the actionPerformed
callbak is called:
yourButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
variable++;
}
});
For more details, you can read the official documentation

- 186
- 8
Here is a quick JButton subclass that does what you want. Yes, it can be done without subclassing, but if you want to modify the behavior of JButton (so it has an additional state like in this case) subclassing is a good alternative.
public class CountButton extends JButton implements ActionListener {
private int count;
CountButton() {
super();
}
@Override
public void actionPerformed(ActionEvent ae) {
++count;
}
public int getCount() {
return count;
}
} // CountButton class

- 18,787
- 4
- 46
- 77