Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: ComboBoxs Help

  1. #1
    Richard5324 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default ComboBoxs Help

    i am trying to make 2 combobox that if u select a sertain item in the 1st combobox the 2nd combobox equal to a sertain value i cant see to get it right can somone help me
    Java Code:
    if (CBRoomType.equals("Single Room"))
            {
       String SingleRoom[] = { "101", "102", "103", "104", "105","106", 
        "107","108", "109", "110", "111", "112", "113", "114", "115",
        "116", "117", "118", "119", "120", "121", "122", "123", "124",
        "125", "126", "127", "128", "129", "130" 
       };
        CBRoomNumber.setItems(SingleRoom);
              CBRoomNumber.setEnabled(true);
            };
            if (CBRoomType.equals("Double Room"));{
        String[] RoomDuble = {"201", "202", "203", "204", "205","206", 
        "207","208", "209", "210", "211", "212", "213", "214", "215",
        "216", "217", "218", "219", "220", "221", "222", "223", "224",
        "225" };
            CBRoomNumber.setItems(RoomDuble);
            CBRoomNumber.setEnabled(true);
        }
           if (CBRoomType.equals("Delux Room"));{
        String[] DeluxRoom = { "301", "302", "303", "304", "305","306", 
        "307","308", "309", "310", "311", "312", "313", "314", "315",
        "316", "317", "318", "319", "320" };
            CBRoomNumber.setItems(DeluxRoom);
            CBRoomNumber.setEnabled(true);
        }
    any help is would be useful thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Richard5324 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: ComboBoxs Help

    i have 2 combo box and when i use the 1st combo box (CBRoomType) and select a thing such as Single room i want the other combo box(CBRoomNumber) to have the a drop down thing to say 101, 102 etc

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ComboBoxs Help

    Thank you for explaining it to me. It makes much better sense to me. So what happens when you tried the code you've posted? Did you get an error? Did the program crash? It did it just not behave as you wanted it to?

  5. #5
    Richard5324 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: ComboBoxs Help

    i get an error on the line
    Java Code:
    CBRoomNumber.setItems(SingleRoom); (Line 8)
    CBRoomNumber.setItems(RoomDuble); (Line 16)
    CBRoomNumber.setItems(DeluxRoom); (Line 23)
    i get get the error "cannot find symbol" i dont think that i right way to do it i will try a different but if u know can u help thank you

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ComboBoxs Help

    Quote Originally Posted by Richard5324 View Post
    i get an error on the line
    Java Code:
    CBRoomNumber.setItems(SingleRoom); (Line 8)
    CBRoomNumber.setItems(RoomDuble); (Line 16)
    CBRoomNumber.setItems(DeluxRoom); (Line 23)
    This makes sense to me because when I checked the JComboBox entry in the API, I couldn't find a setItems(...) method. It looks like you're doing a common mistake that we all do when starting out in Java -- making up a method because it sure would help if one like this exists. So I feel your pain but have to tell you that this never works, and like it or not, we're all stuck using the methods that are available to us as shown by the API and none others.

    Fortunately there is a solution, and that is to work with the combo box's model, a DefaultComboBoxModel to be exact. What I would do in the the first combo box's listener (which could be an ItemListener), is to create a new DefaultComboBoxModel object with one of the three room number arrays depending on the selection made in the first combo box, since this class will accept an Object array as its constructor parameter, and then pass this model into the second JComboBox using its setModel(...) method, and then calling setEnabled(true) on it.

    Please let me know if this confuses you. Best of luck!
    Richard5324 likes this.

  7. #7
    Richard5324 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: ComboBoxs Help

    thank you that helps thanks for the help i will do that now

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ComboBoxs Help

    Quote Originally Posted by Richard5324 View Post
    thank you that helps thanks for the help i will do that now
    You're welcome, and again please tell us if it does or does not work.

Tags for this Thread

Posting Permissions

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