Question

I am trying to add an ActionListener to a JButton created in a loop, then call the ActionListener from another class (the controller class), but its not working. I don't know why.

Here is the first class

public class Browse extends JPanel {

    private JButton play_lists_btn;

    public Browse() {

        int increment = 0;
        while (increment < 5) {
            add(createButton(increment));
            increment++;
        }
    }

    private JButton createButton(final int i) {

        play_lists_btn = new JButton();
        play_lists_btn.setText(" This is " + i);
        return play_lists_btn;
    }

    public void addPlayListener(ActionListener play) {
        play_lists_btn.addActionListener(play);
    }

    public static void main(String args[]) {
        Browse b = new Browse();
        BrowseController bc = new BrowseController(b);
        JFrame frame = new JFrame();
        frame.add(b);
        frame.setSize(1100, 830);
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Here is the controller class that calls the button ActionListener, and creates an ActionEvent for the button

public class BrowseController {

    private Browse b;

    public BrowseController(Browse b) {
        this.b = b;
        b.addPlayListener(new PlayListener());
    }

    private class PlayListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String text = (String) e.getActionCommand();
            System.out.println(text);
        }
    }   
}

Nothing seems to work. the print statement never show up. please help, because I am trying to achieve the MVC design pattern.

Was it helpful?

Solution

try this,

Move your inner class inside Browse.java and add ActionListener for each and every button created

import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Browse extends JPanel {

    private JButton [] play_lists_btn=new JButton[5];//define an array of JButtons

public Browse() {

    int increment = 0;
    while (increment < 5) {
        add(createButton(increment));
        increment++;
    }
}

private JButton createButton(final int i) {

    play_lists_btn[i] = new JButton();
    play_lists_btn[i].setText(" This is " + i);
    return play_lists_btn[i];
}

public void addPlayListener(ActionListener play) {
    for(JButton b : play_lists_btn)
    b.addActionListener(play);
}



public static void main(String args[]) {
    client.Browse b = new client.Browse();
    BrowseController bc = new BrowseController(b);
    JFrame frame = new JFrame();
    frame.add(b);
    frame.setSize(1100, 830);
    frame.setLocationRelativeTo(null);
    frame.setResizable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

BrowseController.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class BrowseController {

private Browse b;

public BrowseController(Browse b) {
    this.b = b;
    b.addPlayListener(new PlayListener());
}

    private class PlayListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String text = (String) e.getActionCommand();
            System.out.println(text);
        }
    }

}

OTHER TIPS

The code in Browse isn't adding an ActionListener to every JButton created.

private JButton playlistButton;

...

private JButton createButton(final int i) {
    // this resets playlistButton every time it's called (before ever adding
    //   an ActionListener to ANY of the buttons)

    playlistButton = new JButton();// creates a NEW JButton EVERY TIME!!!

    playlistButton.setText(" This is " + i);

    return playlistButton; // why does this method return anything?
}

public void addPlayListener(ActionListener play) {
    // this only adds an action listener for the latest value of playlistButton
    //   (not all of the previously created JButton that you don't have a 
    //      reference to anymore).
    playlistButton.addActionListener(play);
}

If you want the Panel to be "separate" from the controller (i.e. MVC), then you'll have to keep track of the state of the Panel. Instead of just storing a single button--you need an array of buttons:

public class Browse extends JPanel{

    private final JButton[] btnArr;

    public Browse(final int numBtns) {
        btnArr = new JButton[numBtns];
        for(int i = 0; i < numBtns; ++i) {
            btnArr[i] = Browse.createButton(i);
            add(btnArr[i]);
        }
    }

    public void addPlayListener(final ActionListener play){
        for(final JButton btn : btnArr)
            btn.addActionListener(play);
    }

    private static JButton createButton(final int i) {
        // create a new JButton, init it AND set action command (if you're 
        //   going to use it)
        final JButton btn = new JButton(" This is " + i);
        btn.setActionCommand(btn.getText());

        return btn;
    }

    public static void main(String args[]) {
        Browse b = new Browse();
        BrowseController bc = new BrowseController(b);
        JFrame frame = new JFrame();
        frame.add(b);
        frame.setSize(1100, 830);
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String text = (String) e.getActionCommand();
        System.out.println(text);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top