I have an ArrayList<JCheckBox> that i want to convert to an ArrayList<String>

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! :)

Answers


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

You can't add a JCheckBox to a List<String>, the types JCheckBox and String are incompatibles.

I guess you want to add the text of the check box into your list, so you have to retrieve it manually, using:

titlesArr.add(titel.getText());

Need Your Help

Get a tablets(iPad or Droid) mac address?

javascript android ipad html5 web-applications

Synopsis: I am developing a HTML5 web app that will allow tablets(iPad or Droid) to login to a server and perform various functions. The client would like a way to check the devices mac address when

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.