Results 1 to 6 of 6
  1. #1
    JustBenj is offline Member
    Join Date
    Aug 2012
    Location
    Ohio
    Posts
    3
    Rep Power
    0

    Default Multiple JComboBox's

    Hello, I'm attempting to use multiple JComboBox's in the same class. The boxes display on screen as planned, however, when handling user input, I'm not sure how to tell which combo box was selected from.

    Here is a screenshot of my program with the boxes being displayed, as they should:
    Multiple JComboBox's-forumhelp1.png

    How would I go about differentiating between a selection made in the "Floor Tiles" box and a selection made in one of the other boxes?

    If possible it would be nifty to be able to make the difference based on what the name of the combo box is.
    For example:
    JComboBox floorBox = new JComboBox(...);
    JComboBox decorativeBox = new JComboBox(...);
    JComboBox gameplayBox = new JComboBox(...);

    and then use a switch statement or something to say if the name is "floorBox", do this with "floorBox's" selection.

    I'm relatively new to Java and programming in general, so any help or advice in greatly appreciated! Thanks!

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

    Default Re: Multiple JComboBox's

    It's simple. when the save button is pressed and you need to extract the information out of the JComboBox, you could simply use the JComboBox variable name. For instance, assuming that they hold Strings, you could do:

    Java Code:
    // in the save JButton's ActionListener's actionPerformed method:
    String floorTile = floorBox.getSelectedValue().toString();
    String decorativeTile = decorativeBox.getSelectedValue().toString();
    String gamePlayTile = gameplayBox.getSelectedValue().toString();

  3. #3
    JustBenj is offline Member
    Join Date
    Aug 2012
    Location
    Ohio
    Posts
    3
    Rep Power
    0

    Default Re: Multiple JComboBox's

    Quote Originally Posted by Fubarable View Post
    It's simple. when the save button is pressed and you need to extract the information out of the JComboBox, you could simply use the JComboBox variable name. For instance, assuming that they hold Strings, you could do:

    Java Code:
    // in the save JButton's ActionListener's actionPerformed method:
    String floorTile = floorBox.getSelectedValue().toString();
    String decorativeTile = decorativeBox.getSelectedValue().toString();
    String gamePlayTile = gameplayBox.getSelectedValue().toString();
    Thank you for your response, however I'm not sure this does what I wish to accomplish. I probably explained it a little wrong lol. I'm needing to perform an action when a selection in one of the JComboBox's is made. The action performed would be different depending on which combo box was selected from, and the selection that was made. How would I go about setting up my itemStateChanged() method to do something different depending on which combo box was selected from, and the selection that was made?

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

    Default Re: Multiple JComboBox's

    Quote Originally Posted by JustBenj View Post
    Thank you for your response, however I'm not sure this does what I wish to accomplish. I probably explained it a little wrong lol. I'm needing to perform an action when a selection in one of the JComboBox's is made. The action performed would be different depending on which combo box was selected from, and the selection that was made. How would I go about setting up my itemStateChanged() method to do something different depending on which combo box was selected from, and the selection that was made?
    You should probably give each JComboBox its own ItemListener, and then there would be no chance for confusion. An anonymous inner class would work well for this.

  5. #5
    JustBenj is offline Member
    Join Date
    Aug 2012
    Location
    Ohio
    Posts
    3
    Rep Power
    0

    Default Re: Multiple JComboBox's

    Quote Originally Posted by Fubarable View Post
    You should probably give each JComboBox its own ItemListener, and then there would be no chance for confusion. An anonymous inner class would work well for this.
    Oh, I hadn't even thought of using an anonymous inner class. That will probably work perfectly. Thanks!

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

Similar Threads

  1. Multiple JComboBox
    By LiLGuy2004 in forum AWT / Swing
    Replies: 5
    Last Post: 07-28-2012, 06:05 AM
  2. One process open multiple threads on multiple CPUs
    By kfcnhl in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-12-2011, 09:27 AM
  3. multiple Data from Jsp that having same name of multiple input tage to servlet
    By rahul9323.2007@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 07-29-2011, 02:00 PM
  4. problem w/ multiple jcombobox
    By garagumo in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2011, 07:47 AM
  5. Replies: 2
    Last Post: 12-10-2009, 02:58 PM

Posting Permissions

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