Results 1 to 3 of 3
  1. #1
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Exclamation [SOLVED] Referencing Variable within Loop

    I am near to completing my application which has been somewhat challenging, it is due friday. I have all of my Labels etc nicely within Frames. my only problem is that only 1 of my Checkboxes are displaying instead of the 8 that should.

    For what it's worth this is the parralel array and Checkbox init code

    Java Code:
    //parralel Array
    final String TURBINE_NAME []
    = {"Judoon 3X", "Gallifrey 3000", "Veridian AX25", 
    "Kasterborous M4", "Axxon 300","Dradis CF9", "Kenobi 0B1", "J-Dorian"};
    
    CheckboxGroup Wind_Turbine_CheckboxGroup
    = new CheckboxGroup ();
    
    Checkbox      Wind_Turbine_Checkbox []
    = new Checkbox [TURBINE_NAME.length];
    
    Checkbox       Hidden_Checkbox
    = new Checkbox ("", true,  Wind_Turbine_CheckboxGroup);
    Java Code:
    for (int i = 0; i < TURBINE_NAME.length; i++)
     {
                          
    Wind_Turbine_Checkbox [i] = new Checkbox (TURBINE_NAME [i], false, Wind_Turbine_CheckboxGroup);
    Wind_Turbine_Checkbox [i].addItemListener(this);
    
    Panel CheckboxPanel = new Panel (new GridLayout (4, 2, 1, 1));
    CheckboxPanel.add (Wind_Turbine_Checkbox [i]);
     
    Panel ApplicationPanel = new Panel (new GridLayout (4, 1, 10, 1));
    	         ApplicationPanel.add (TurbineLayoutPanel);
    	         ApplicationPanel.add (CheckboxPanel);
    	         ApplicationPanel.add (choice_panel);
                          ApplicationPanel.add (DisplayPanel);
     
    	          add (ApplicationPanel);
    }
    This does everything I need except it only displays one of the Checkboxes. I tried referencing the Wind_Turbine_Checkbox outside the loop, but it just errors and says it cannot find the variable. I am seriously out of ideas.

    If anyone can see whats going on here that would be greatly appreciated.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    Panel CheckboxPanel = new Panel (new GridLayout (4, 2, 1, 1));
    for (int i = 0; i < TURBINE_NAME.length; i++)
    {
        Wind_Turbine_Checkbox [i] = new Checkbox (TURBINE_NAME [i], false,
                                                  Wind_Turbine_CheckboxGroup);
        Wind_Turbine_Checkbox [i].addItemListener(this);
        // Making a new panel each time will only have the last checkBox in it.
        //Panel CheckboxPanel = new Panel (new GridLayout (4, 2, 1, 1));
        CheckboxPanel.add (Wind_Turbine_Checkbox [i]);
    }
    Panel ApplicationPanel = new Panel (new GridLayout (4, 1, 10, 1));
    ...
    ApplicationPanel.add (CheckboxPanel);
    ...
    add (ApplicationPanel);

  3. #3
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default

    I swear I tried doing what you put, hmm must have been something else i was doing wrong. Cheers for that it works perfectly :)

Similar Threads

  1. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:44 PM
  2. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 07:44 AM
  3. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM
  4. Referencing JPanel Class
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-12-2007, 02:31 PM
  5. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •