Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: JComboBox Issue

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy JComboBox Issue

    I'm trying to get the input of several JComboBoxes and execute a statement depending on which option the user chose for example if destination combo Box = japan duration combo box = 10 days and hotel combox = hotel then print line "welcome" I have tried many options but not getting the desired outcome. at the moment I'm using an if statement with only 2 of 3 of the J combo boxes but it's not working here's my code:

     String[] Dest = new String[] {"Select Destination","Crete", "Paris", "Croatia"};  
            String[] Accomodation = new String[] {"Select your Accomodation","Hotel", "Villa", "Bed in Breakfast","Youth Hostel"};
            JComboBox<String> comboDest1 = new JComboBox<String>(Dest);
            JComboBox<String> comboAccom1 = new JComboBox<String>(Accomodation);
     
            comboDest1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    //
                    // Get the source of the component, which is our combo
                    // box.
                    //
                	JComboBox comboDest1 = (JComboBox) event.getSource();
                    JComboBox comboAccom1 =(JComboBox) event.getSource();
     
                    Object selected = comboDest1.getSelectedItem();
                     Object selectedForAccom1 = comboAccom1.getSelectedItem();
                    if(comboDest1.getSelectedItem().equals("Crete") && comboAccom1.getSelectedItem().equals("Hotel"))
                    {
                        System.out.println("hey");
                    }
     
     
                }
            });
    any help?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JComboBox Issue

    Look at these two lines:

    JComboBox comboDest1 = (JComboBox) event.getSource();
    JComboBox comboAccom1 =(JComboBox) event.getSource();

    That's not going to work. The getSource() function returns the *single* component (in this case JComboBox) that resulted in the ActionEvent. In this case, it's whatever JComboBox you made the selection in.

    You have to refer directly to the two JComboBoxes instead of trying to go through the getSource() function. You can do that by either passing them into the ActionListener class, or since you're using an anonymous inner class, referring to them directly. Note that with the latter approach, you'll have to make the variables final.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox Issue

    Thanks but how would i refer directly to the JComboBoxes? (Sorry I'm a beginner)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JComboBox Issue

    Read the tutorial on anonymous classes: Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    How would you use those variables normally? Do the same thing in the anonymous class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox Issue

    Strange. after deleting those two lines and making the variables final it works but only when i input the accom1 combobox first and then the dest1 combobox after.

    Edit: after reading through that link I'm no more closer in understand how it's done do you have any other links in more layman's terms? or even a youtube video.
    Last edited by cloudracer; July 24th, 2014 at 12:06 PM. Reason: added info, corrected spelling

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JComboBox Issue

    Here is an example:

    final JButton button = new JButton("Click me");
     
    button.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
          button.setText("You clicked me.");
       }
     
    });

    This code creates a JButton whose text changes when you click it. It uses an anonymous inner class to create an ActionListener, and inside that ActionListener, it refers directly to the button variable. It's able to do that because the button variable is final.

    If you're confused about something more specific, ask a more specific question and we'll try to help.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    cloudracer (July 24th, 2014)

Similar Threads

  1. [SOLVED] Having trouble with a JComboBox in a JFileChooser. Issue with removing elements and items.
    By GoodbyeWorld in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 11th, 2013, 05:13 PM
  2. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  3. Replies: 48
    Last Post: April 3rd, 2012, 03:26 PM
  4. JComboBox
    By SV25 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 20th, 2011, 08:47 AM
  5. [SOLVED] JCombobox help
    By sman36 in forum AWT / Java Swing
    Replies: 10
    Last Post: August 11th, 2010, 04:11 AM

Tags for this Thread