adrian quevada adrian quevada - 2 years ago 149
Java Question

How to add actionListener to each Button

It might be confusing for some to answer this but I will try to put my question in the best way. I am working with jdbc and gui. Basically I want to display (in buttons format) the particular data received from my sql database. I could get the data correctly and put it to my array of buttons as their names. In other words, I have an ArrayList of buttons with different names/texts received from my database. Thus i really need to make an arraylist of buttons since data are dynamically populated. My problem is, I am so confused of how am going to create an actionListener to each button. Everytime each button is clicked, it must show the values associated with its name. I don't know how am i supposed to pass at least the names of the buttons to my actionListener method (or action Event Handler). If you find it confusing, here is the code for my buttons.

todayTaskButton.add(new JButton(taskForToday.get(i)));
todayTaskButton.get(i).setPreferredSize(new Dimension(300,75));
todayTaskButton.get(i).setFont(new Font("Century Gothic",Font.PLAIN,30));

Thank you so much

Answer Source

Honestly there are so many ways you might achieve this, the problem is picking the right one of you...

You could...

Create a anonymous class for each button, each time your create them

todayTaskButton.get(i).addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {

While this can work, it can make the code really messy, you also still need a way to map the action back to the button in some way, which can be done using the actionCommand property or using the source property if you don't mind iterating through the list of available buttons

You could...

Create a purpose build class which implements ActionListener for each button, which possibly takes some kind of reference to the data

todayTaskButton.get(i).addActionListener(new TodayActionListener(taskForToday.get(i)));

This is a little more focused, as you don't really care about the button, as you have the "today" value for the listener, so all the normally repeated code could be isolated into a single class and you would simply pass in the "variable" element

You could...

Take full advantage of the Action API and make individual, self contained actions for each button...

public class TaskAction extends AbstractAction {

    public TodayAction(String task) {
        putValue(NAME, task);

    public void actionPerformed(ActionEvent e) {
        // Specific action for task


Then you could simply use

todayTaskButton.add(new JButton(new TaskAction(taskForToday.get(i))));

While this is similar to the previous option, the Action is a self contained unit of work and has a number of properties which the JButton can use to configure it self. The Action can also be re-used for JMenuItems and key bindings, making it incredibly flexible

Have a closer look at How to Use Actions for more details

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download