I have an ArrayList<JCheckBox>
that i want to convert to an ArrayList<String>
First I do like this. I get all my titles from a file and put them into a new ArrayList. Afterwords I make a new JCheckBox Array that contains all the Strings from the StringArray.
ArrayList<String> titler = new ArrayList<String>();
titler.addAll(FileToArray.getName());
ArrayList<JCheckBox> filmListe = new ArrayList<JCheckBox>();
for(String titel:titler){
filmListe.add(new JCheckBox(titel));
}
for(JCheckBox checkbox:filmListe){
CenterCenter.add(checkbox);
}
This is what I am trying to do: First I make a new ArrayList (still in the JCheckBox format), that contains alle the selected Checkboxes. Afterwords I want to add the to a new ArrayList in a String format.
The main problem is italicized (with **):
ArrayList<JCheckBox> selectedBoxes = new ArrayList<JCheckBox>();
for(JCheckBox checkbox: filmListe){
if (checkbox.isSelected()){
selectedBoxes.add(checkbox);
}
ArrayList<String> titlesArr = new ArrayList<String>();
for(JCheckBox titel:selectedBoxes){
*titlesArr.add(titel);*
}
A lot of code and text for a little problem! But I really appreciate your help! :)
Assuming the checkbox's label is exactly the same as what you originally had in your list of titles, just use the checkbox's getText
method (which gets the String label). You don't need to make a separate list of checkboxes that are checked - just put an if-block inside your first loop like this:
ArrayList<String> titlesArr = new ArrayList<String>(filmListe.size());
for (JCheckBox checkbox : filmListe) {
if (checkbox.isSelected()) {
titlesArr.add(checkbox.getText());
}
}