12 Replies Latest reply: Oct 18, 2006 7:04 AM by 807598 RSS

    State of checkboxes in array

    807598
      Hey. I'm trying to get the state of the checkboxes in an array of items using the following code:
      boolean [ ] pState = false[ ];  //"illegal start of expression here. ']' expected 
      public boolean getCheckBoxState()
       {
         for (int i = 0; i < contract.getNRentalItems(); i ++)
         {
            JCheckBox jCheckBox = rentalItemCheckboxes;
      if (jCheckBox.isSelected())
      {
      pState[i] = true;
      }
      else
      {
      pState[i] = false;
      }
      }
      return pState[i];
      }

      What am I doing wrong?
        • 1. Re: State of checkboxes in array
          791266
          Hey. I'm trying to get the state of the checkboxes in
          an array of items using the following code:
          The first line should read:

          boolean [ ] pState = new boolean[length ];

          Kaj
          • 2. Re: State of checkboxes in array
            807598
            Thanks for the quick reply...
            The first line should read:

            boolean [ ] pState = new boolean[length ];
            Should I be using "length" as you have or is this supposed to be replaced by something else? Cos it's saying that the variable doesn't exist.
            • 3. Re: State of checkboxes in array
              791266
              Thanks for the quick reply...
              The first line should read:

              boolean [ ] pState = new boolean[length ];
              Should I be using "length" as you have or is this
              supposed to be replaced by something else? Cos it's
              saying that the variable doesn't exist.
              It should be replaced with contract.getNRentalItems()
              • 4. Re: State of checkboxes in array
                807598
                Now I've changed the first line as you advised, & the last line but I get an error at runtime: "java.lang.ArrayIndexOutOfBoundsException: 4
                at nz.ac.massey.rental.RentalShopWindow.getCheckBoxState(RentalShopWindow.java:217)
                at nz.ac.massey.rental.Image.run(Image.java:28)

                My code now:
                boolean [] pState = new boolean[contract.getNRentalItems()];
                
                 public boolean getCheckBoxState()
                 {
                   for (int i = 0; i < contract.getNRentalItems(); i ++)
                   {
                     JCheckBox jCheckBox = rentalItemCheckboxes;
                if (jCheckBox.isSelected())
                {
                pState[i] = true;
                }
                else
                {
                pState[i] = false;
                }
                }
                return pState[contract.getNRentalItems()];
                }
                • 5. Re: State of checkboxes in array
                  791266
                  Which line is line 217?

                  You are trying to access an element with an index larger than the array size.

                  Kaj
                  • 6. Re: State of checkboxes in array
                    807598
                    This is line 218:
                    return pState[contract.getNRentalItems()];
                    i.e. last line

                    It's happening when In try to call this method from a thread class, the code of which is:
                    public class Image
                        extends Thread
                      {
                        String imageName = "";
                        Contract contract = new Contract();
                        RentalShopWindow rentalShopWindow;
                        public Image(RentalShopWindow rentalShopWindow) 
                        {
                          this.rentalShopWindow = rentalShopWindow;
                        }
                    
                        public void run()
                        {
                          while (isAlive())
                          {
                            for (int i = 0; i < contract.getNRentalItems(); i++)
                            {          
                              if (rentalShopWindow.getCheckBoxState() == false)
                              {
                                try
                                {
                                  Thread.sleep(3000);
                                  imageName = contract.getRentalItem(i).getImageFileName();
                                  System.out.print(imageName);
                                  rentalShopWindow.setImageIcon(new ImageIcon(this.getImageName()));
                                  rentalShopWindow.setImageName(contract.getRentalItem(i).
                                                                getDescription());
                                }
                                catch (InterruptedException e)
                                {
                                  return;
                                }
                              }
                              else
                              {
                                imageName = "default.gif";
                              }
                            }
                          }
                        }
                      
                      public String getImageName()
                        {
                          return imageName;
                        }
                      }
                    • 7. Re: State of checkboxes in array
                      791266
                      return pState[contract.getNRentalItems()];
                      That's clearly wrong? Why do you want to return only one element? The size of the list is e.g. 4. That means that the indices 0, 1, 2 and 3 are valid. 4 is not valid

                      Kaj
                      • 8. Re: State of checkboxes in array
                        807598
                        Ok, I've popped "3" in there:
                        return pState[3];
                        Which doesn't give me any errors. Thanks! But it's still not doing what I want it to do which is get the state for each item in the array...
                        • 9. Re: State of checkboxes in array
                          791266
                          Ok, I've popped "3" in there:
                          return pState[3];
                          Which doesn't give me any errors. Thanks! But it's
                          still not doing what I want it to do which is get the
                          state for each item in the array...
                          Shouldn't you return the array in that case, and not just one boolean?
                          • 10. Re: State of checkboxes in array
                            807598
                            how would I do that then?
                            • 11. Re: State of checkboxes in array
                              791266
                              Declare the method as

                              public boolean[] getCheckBoxState()

                              and return pState

                              Kaj
                              • 12. Re: State of checkboxes in array
                                807598
                                Declare the method as

                                public boolean[] getCheckBoxState()

                                and return pState
                                How, in the Thread class, will I be able to check what the return value of this method is?
                                Error: "operator == cannot be aplied to boolean [], boolean"