1

I have 20 loop-generated JToggleButtons and I need to count how many of them are active.

private void generarBotones(){    
    JToggleButton b;
    this.panelCuerpo.setLayout(new GridLayout(4,5));        
    for(int i = 1; i<=20; i++){
        b = new JToggleButton();
        b.setText(String.valueOf(i));
        this.panelCuerpo.add(b);
        b.addActionListener(new ActionListener() {
            int clicks = 0;
            @Override
            public void actionPerformed(ActionEvent ae2){
                clicks = clicks + 1;
                System.out.println(clicks);
            }                
            public void setCantidadBoletas(int clicks){
                cantidadBoletas = clicks;
            }
        });
    }   
}

The problem here is that it counts how many times is EACH ONE of them clicked instead of count how many of them are selected.

PS. I tried to use (b.isSelected()) but b needs to be final to access it so it wasn't the solution.

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
Andrés Buitrago
  • 205
  • 1
  • 2
  • 9
  • If you declare your JToggleButton inside the loop, then it can be final. But even better, just use an ArrayList of JToggleButtons as a field and iterate through that. – Hovercraft Full Of Eels May 30 '15 at 15:51

4 Answers4

2

If you declare the JToggleButton inside the loop, you can make it final:

for (int i = 1; i<=20; i++) {
    JToggleButton b = new JToggleButton();

Then you can use b.isSelected:

    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (b.isSelected())
                clicks++;
            else
                clicks--;
        }
    });
}

clicks would have to be a class variable.

tbodt
  • 16,609
  • 6
  • 58
  • 83
1

Create a class attribute that will count the selected toggles:

private int selectedCount;

Initialize the counter to 0 in your constructor:

this.selectedCount = 0;

Increment or decrement the counter every time the state of a toggle changes:

b.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent ev) {
        if (ev.getStateChange() == ItemEvent.SELECTED){
             YourClass.this.selectedCount++;
        } else if (ev.getStateChange() == ItemEvent.DESELECTED){
             YourClass.this.selectedCount--;
        }
        System.out.println(YourClass.this.selectedCount);
    }
});
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
1

Suggestions:

  • Create a field, JToggleButton[] toggleButtons = new JToggleButton[20]
  • Or use an ArrayList if you so choose
  • In your for loop create your JToggleButton and assign it to the proper array item.
  • In the ActionListener simply iterate through the array, counting how many of its JToggleButton items are selected.
  • You're done.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

There are many ways to get this done and the best way depends on the rest of your code. I tried to keep it as close to yours.

You can just declare the buttons as final inside the loop and keep a global count of the number of buttons selected, which will be modified in the ActionListener:

public class ButtonsCount extends JFrame {

    int clicks = 0;

    ButtonsCount() {

        JLabel label = new JLabel("0");
        JPanel buttonsPanel = new JPanel(new GridLayout(4,5));

        for(int i = 1; i <= 20; i++) {
            final JToggleButton b = new JToggleButton();
            b.setText(String.valueOf(i));
            buttonsPanel.add(b);
            b.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae2){
                    if (b.isSelected())
                        label.setText(String.valueOf(++clicks));
                    else
                        label.setText(String.valueOf(--clicks));
                }
            });
        }

        add(buttonsPanel);
        add(label, BorderLayout.PAGE_END);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {

        new ButtonsCount();
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74