How to capture the amount of clicks over a group of loop generated JToggleButtons?

Andrés Buitrago

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.

tbodt

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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to replace character in capture group with sed

분류에서Dev

in excel how change the value of a cell if it is over a specified amount

분류에서Dev

How to loop over the lines of a file?

분류에서Dev

perl: how loop over a hash

분류에서Dev

How to run a loop until an exception is not generated

분류에서Dev

How to hide submenu when user clicks option, but still display when user hovers over menu

분류에서Dev

How to loop over verticies in Gremlin and adding edge

분류에서Dev

How to loop over numbered files and also the outputs?

분류에서Dev

How to correctly loop over my JSON result?

분류에서Dev

How to pass a variable generated from foreach loop to another page in PHP

분류에서Dev

How do I loop over output from shuf?

분류에서Dev

How to loop over piped lines and use lines as variable in bash?

분류에서Dev

How to loop over images in media library and display them? (wordpress)

분류에서Dev

Alternative capture group numbering in JavaScript

분류에서Dev

How To Limit The Amount Of Notifications

분류에서Dev

How to I find the location within a string where a match was found for a particular capture group?

분류에서Dev

Best practice: Number of views/clicks over the last 30 days

분류에서Dev

Loop over ModelView in razor

분류에서Dev

Too many alerts generated in for loop

분류에서Dev

How to loop through columns of two dataframes, group by key & perform calculation

분류에서Dev

LOOP AT ... GROUP BY with dynamic group key

분류에서Dev

Perl search and replace with variable and capture group

분류에서Dev

Typo3: capture default generated css in lib

분류에서Dev

How to convert Dutch amount in English?

분류에서Dev

MySQL group by loop bash

분류에서Dev

LibGDX How to know if anybody clicks on texture

분류에서Dev

Loop over function to build list

분류에서Dev

Loop over two arrays in Twig

분류에서Dev

Loop over a Numpy array with Cython

Related 관련 기사

  1. 1

    How to replace character in capture group with sed

  2. 2

    in excel how change the value of a cell if it is over a specified amount

  3. 3

    How to loop over the lines of a file?

  4. 4

    perl: how loop over a hash

  5. 5

    How to run a loop until an exception is not generated

  6. 6

    How to hide submenu when user clicks option, but still display when user hovers over menu

  7. 7

    How to loop over verticies in Gremlin and adding edge

  8. 8

    How to loop over numbered files and also the outputs?

  9. 9

    How to correctly loop over my JSON result?

  10. 10

    How to pass a variable generated from foreach loop to another page in PHP

  11. 11

    How do I loop over output from shuf?

  12. 12

    How to loop over piped lines and use lines as variable in bash?

  13. 13

    How to loop over images in media library and display them? (wordpress)

  14. 14

    Alternative capture group numbering in JavaScript

  15. 15

    How To Limit The Amount Of Notifications

  16. 16

    How to I find the location within a string where a match was found for a particular capture group?

  17. 17

    Best practice: Number of views/clicks over the last 30 days

  18. 18

    Loop over ModelView in razor

  19. 19

    Too many alerts generated in for loop

  20. 20

    How to loop through columns of two dataframes, group by key & perform calculation

  21. 21

    LOOP AT ... GROUP BY with dynamic group key

  22. 22

    Perl search and replace with variable and capture group

  23. 23

    Typo3: capture default generated css in lib

  24. 24

    How to convert Dutch amount in English?

  25. 25

    MySQL group by loop bash

  26. 26

    LibGDX How to know if anybody clicks on texture

  27. 27

    Loop over function to build list

  28. 28

    Loop over two arrays in Twig

  29. 29

    Loop over a Numpy array with Cython

뜨겁다태그

보관