Search code examples
javajcheckbox

Creating an object in a for loop, and trying to access it elsewhere


I am trying to create a program to shuffle music. I have already gotten all the songs imported, and now I am trying to create a ``JCheckBox for each song. I have a for loop that creates these checkboxes, but I need a way to test if the box is checked. I am already using if(box.isSelected()), but need to discern which box, and need to access box outside the for loop. Here is my code. By the way, songs is an ArrayList.

public static void checkboxList() {
    ArrayList<JCheckBox> checkboxes = new ArrayList<>();

    for (String element : songs) {
        System.out.println("Reached checkbox thing");
        System.out.println(element);
        JCheckBox box = new JCheckBox(element);
        checkboxes.add(box);
        panel.add(box);
        frame.pack();
    }
    int loop = 0;
    while (loop == 0) {
        if (checkboxes.contains(box.isSelected())) {

        }
    }
}

Solution

  • As said by everyone you can add an event listener to identify the object which generated the event.

    public class evttest implements ItemListener{
    
    //Declared your arraylist checkboxes global. To access it in other part of code
    static ArrayList<JCheckBox> checkboxes ;
    
    public evttest(){
    
    ...
    
    checkboxList();
    //After calling checkboxesList() call add method to add itemlistener to each of them
    this.add();
    }
    
    
    public static void checkboxList() {
    checkboxes = new ArrayList<>();
    JCheckBox box;
    
    for (String element : songs) {
        System.out.println("Reached checkbox thing");
        System.out.println(element);
        box = new JCheckBox(element);
    
        checkboxes.add(box);
        panel.add(box);
        frame.pack();
    }
    
    frame.getContentPane().add(panel);
    }
    
    //this method adds ItemListener to all the 
    //checkboxes you have in your ArrayList checkboxes
    public void add(){
        for(JCheckBox cb : checkboxes){
            cb.addItemListener(this);
        }
    }
    
    
    
    @Override
    public void itemStateChanged(ItemEvent e) {
    
        //If the item is selected then do something
        if(e.getStateChange() == ItemEvent.SELECTED){
    
            //cb is the checkbox on which your event occured.
            //You can then use cb to perform your required operation
    
            JCheckBox cb = (JCheckBox)e.getSource();
            System.out.println("clicked item is: " + cb.getText());
        }
    }
    
    public static void main(String...args){
        new evttest();
        }
    
    
    }