-2

The for loop isn't working. I get "Choice cannot be resolved to a variable". I would like to add a group of options to multiple Choice objects added to the generics list. In this example you can see one of these objects (DidWell1) .

import java.awt.Choice;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;


public class testing {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    testing window = new testing();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public testing() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Choice DidWell1 = new Choice();
        DidWell1.setBounds(119, 36, 135, 20);
        frame.getContentPane().add(DidWell1);

        List<Choice> list = new ArrayList<Choice>();

        list.add(DidWell1);

        String option = "Friday";

        For(Choice choice: list){
            choice.add( option );
        }
    }

}
user2078674
  • 165
  • 2
  • 2
  • 14

1 Answers1

1

Create a list of Choice objects

List<Choice> list = new ArrayList<Choice>();

Next add all Didwells to this list.

list.add( didwell1 ); etc

With this setup adding new options can be done using a For loop:

String option = "Friday";

for( Choice choice: list ){
    choice.add( option );
}

Or with a While loop:

Iterator<Choice> iter = new list.iterator();
while iterator.hasNext(){
    iter.next().add( option );
}
avk
  • 871
  • 1
  • 9
  • 22
  • I'm get this error. "Choice cannot be resolved to a variable" at the Foreach line – user2078674 Apr 26 '15 at 16:27
  • if Choice fails again, it should be equal to the classname of your dropdownchoice object. My guess was that Java.set.Choice was the classname. – avk Apr 26 '15 at 16:31
  • the import is, java.awt.Choice; when I try to type "for( java.awt.Choice choice" it doesn't find it... – user2078674 Apr 26 '15 at 16:39
  • Autocorrect error. I meant awt. Tell me how you declared your Didwells. E.g. Choice didwell1 = new Choice(); – avk Apr 26 '15 at 16:40
  • final Choice DidWell8 = new Choice(); Later I want to use the DidWell8.getSelectedItem(). Without final it doesn't work. – user2078674 Apr 26 '15 at 16:45
  • Assuming you are working with java version 5 or up, the code in my example should work. Check your code for typos. – avk Apr 26 '15 at 16:47
  • well, it says jre7 and ive stripped it down to just that choice bit. (see edited question) still no luck :/ – user2078674 Apr 26 '15 at 17:17
  • Update your question with the actual code you have so far. – avk Apr 26 '15 at 17:23
  • my bad. Autocorrect again."For" should be "for", i.e. no capital F. I have updated the answer – avk Apr 26 '15 at 17:37