0

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())) {

        }
    }
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
user4775991
  • 127
  • 2
  • 14
  • would make more sense to simply add an `ActionListener`, though if you prefer this way, you'll have to iterate over all checkboxes. –  Jun 30 '15 at 22:50
  • I got to agree with Paul here, I think what you really need is an ActionListener: [Linkage](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html) – markspace Jun 30 '15 at 22:52

2 Answers2

0

If you need unique identifiers, why not create your own JCheckbox?

Something like:

 public class MyCheckBox extends JCheckBox {

 public MyCheckBox(paramOne, paramTwo, paramN) {
     super();    
    //do stuff with the unique identifiers 
 }

 //inherit all methods from super class
 @Override
 public boolean isSelected() {
     return super.isSelected();
 }

  //ETC...
 }
andrewdleach
  • 2,458
  • 2
  • 17
  • 25
0

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();
    }


}
bonney
  • 455
  • 4
  • 7
  • Thank you, this all looks fine. The one issue I get is that the this.add() does not work, because I am calling it and checkboxList() in a static function. However, I need the function to be static. Any ideas? – user4775991 Jul 01 '15 at 11:46
  • If you get a chance, @bonney, could you try to answer my above question? I need to get my program finished soon. – user4775991 Jul 02 '15 at 11:45
  • Nevermind, I figured it out. If your class name is Drivers, type Drivers instance = new Drivers(); Then just do instance.add(); – user4775991 Jul 03 '15 at 12:26