Problem JButton JavaSwing

1

I had a question about the JButton component. To implement what I need, I use a variable number of buttons (All are equal in terms of features (button name, function etc)) and as all that is the same, I thought of implementing it as an array of buttons that will be created automatically and will also be automatically placed on the screen. The problem is that the number of buttons that I have to generate is variable (Sometimes I need 4 buttons other 8 etc) I tried to implement it in this way but of course I have the problem of the action listener and I do not know how to solve it

public class PruebaBotones extends JFrame implements ActionListener {

private JButton[] aBotones = new JButton[3]; //Array de botones
private JLabel mensaje;
private int posY = 20;

public PruebaBotones()
{

    for(int i = 0; i < 3; i++)
    {
        aBotones[i] = new JButton("Boton "+(i+1));
        aBotones[i].setSize(300 , 30);
        aBotones[i].addActionListener(this);
    }

    for(int i = 0; i < 3; i++)
    {
        aBotones[i].setLocation(20 , posY);
        add(aBotones[i]);
        aBotones[i].setVisible(true);

        posY = posY + 50; //Variamos la posicion Y para que se situe uno debajo del otro

    }

Once I have created the buttons and their operation I have decided to do the ActionListener, as is an example, the only thing I do is modify a label to check that the event is done correctly

public void actionPerformed(ActionEvent actionEvent) {

    if(actionEvent.getSource() == aBotones[0]) //Pulsamos el 1 boton
    {
        mensaje.setText("Boton pulsado 1");

    } //Lo mismo para los demas casos
    if(actionEvent.getSource() == aBotones[1]) //Pulsamos el 2 boton
    {
        mensaje.setText("Boton pulsado 2");
    }
    if(actionEvent.getSource() == aBotones[2]) //Pulsamos el 3 boton
    {
        mensaje.setText("Boton pulsado 3");
    }

}

Until there everything is correct and it works, the problem comes when I need more elements than I have defined in the ActionListener because the array of buttons I have defined it as a fixed number (In my case 3) but of course I need 5 or 9 or 1 or whatever and if that happens I have no way to change the ActionListener.

Any ideas on how to implement this in the same way as I have or similar to perform the same function?

I had thought of something like that but I get an error.

    for(int i = 0; i < 3; i++)
    {
        aBotones[i] = new JButton("Boton "+(i+1));
        aBotones[i].setSize(300 , 30);
        aBotones[i].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                mensaje.setText("Boton pulsado "+i);
            }
        });
    }

Thank you very much for the help.

    
asked by Josemanuu 08.05.2018 в 18:38
source

1 answer

0

I think the solution is simple, and it's doing a for loop inside actionPerformed .

@Override
    public void actionPerformed(ActionEvent e) {

        for (int i = 0; i < aBotones.length; i++) 
            if(e.getSource() == aBotones[i]) //Pulsamos el boton correspond.
                mensaje.setText("Boton pulsado " + (i+1));
    }

P.d. I've done the test and it works perfectly.

    
answered by 08.05.2018 / 19:49
source