Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:41:04+00:00 2026-06-02T19:41:04+00:00

I have two classes, a gui class that contains two combobox’s and a listener

  • 0

I have two classes, a gui class that contains two combobox’s and a listener class that listens to the two combobox’s. The software is about pizza’s and the combobox’s are to select pizza type and quantity, I’ll post the relevant code.

(gui class)
private Listener listen = new Listener();
private JComboBox chooseItem = new JComboBox();
private JComboBox quantity = new JComboBox();
private String[] selection = {"Choose a Pizza","Margherita", "Pepperoni", "Four Seasons", "Chips", "Garlic Bread", "Potato Wedges", "Cocacola", "Orange Juice", "Lemonade"};
private String[] qSelection = {"0","1","2","3","4","5","6","7","8","9","10"};

private void comboBoxs() {
 choosePizza = new JComboBox(selection);
    chooseItem.setSelectedIndex(0);
    panel.add(choosePizza);
    choosPizza.addActionListener(listener);

 quantity = new JComboBox(qSelection);
    quantity.setSelectedIndex(0);
    panel.add(quantity);
    quantity.addActionListener(listener);
}

In the listener class I am stuck and have not been able to find a solution as of yet. I have to use an ActionListener but I am unsure as to how I am meant to differentiate between the two combobox’s.

(listener class)

public void actionPerformed(ActionEvent event) {

   if (actionCommand.equals("Confirm")) {
          String q = new String();
          String d = new String();
            d = description;
            q = quantity;
    }

 else {
        JComboBox cbq = (JComboBox)event.getSource();
        String itemNumber = (String)cbq.getSelectedItem();
        getQuantity(itemNumber);

        JComboBox cb = (JComboBox)event.getSource();
        String name = (String)cb.getSelectedItem();
        getItemName(name);
   }
 }

  public String getItemName(String name) {
        description = new String();
        description = name;
        return name;
  }

  public String getQuantity(String itemNumber){
        quantity = new String();
        quantity = itemNumber;
        return itemNumber;
 }

So, to summarize: when I select values in the two JComboBox’s I want to be able to store the selected chooseItem JComboBox item in the “description” string and the selected quantity JComboBox item in the “quantity” string.

Currently the chooseItem JComboBox selected item appears as both values and the quantity does not appear at all. (This is actually progress as before whichever box was used last appeared in the description string and quantity pointed to null.)

All the examples I’ve been able to find online only ever involve an ActionListener with a single JComboBox, I’ve been trying to get it to work for hours but have had no luck, I’m completely stuck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:41:05+00:00Added an answer on June 2, 2026 at 7:41 pm

    You either attach a separate listener to each combobox, so your listener knows that the events its receives originated from that specific combobox.

    The other solution is to give the listener access to those comboboxes (either by exposing them in your class, or by passing them as parameters to the constructor of your listener), and compare the source of the event with those comboboxes to determine which combobox caused the event.

    Personally I prefer a one-to-one mapping between my listeners and my components to avoid such complications.

    Small side-note: there are some weird things in the code you posted

    • public void getQuantity(String itemNumber): the void conflicts with the fact you actually return something -> won’t compile
    • In your listener you ask the event twice for its source and cast it to two different comboboxes. This is of course incorrect

      JComboBox cbq = (JComboBox)event.getSource();
      String itemNumber = (String)cbq.getSelectedItem();
      getQuantity(itemNumber);
      
      //this returns the same combobox as above, so probably incorrect behavior
      JComboBox cb = (JComboBox)event.getSource();
      String name = (String)cb.getSelectedItem();
      getItemName(name);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that don't know anything about themselfs class A, class B.
I have two classes(Book,Software) that inherit from the Product class. these two classes have
Say I have two classes, and neither of them are GUI components. Class A
I have two classes that extend the activity class. Each class has it's own
I have two classes that are derived from an abstract generic class, which is
I have two classes that refer to each other, but obviously the compiler complains.
I have two classes that I would like to merge into a composite. These
I have two classes in a PHP application, B extending A for example. Class
If have two classes, Class A and Class B , B is a subclass
I have two classes that I am testing (let's call them ClassA and ClassB).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.