Hi all,

I'm new to Java and going through a steep learning curve. Every now and then I'm getting dead stuck, so your help is immensely appreciated.

So, here's the problem:

(with Java NetBeans IDE)
I have a JFrame form, and in it 4 JComboBox'es. Also a JButton titled "Enter".

So when I click on the "Enter" button I want to read the selected ComboBox Text for all the ComboBoxes.

e.g.

if ComboBox 1 = "Ford"
ComboBox 2 = "Focus"
ComboBox 3 = "Red"
ComboBox 4 = "2005"

then upon clicking the "Enter" button I would like to have something like this

carstring = "Ford Focus Red 2005"

How do I do this?
Further I want to have the possibility of not having to click on each combobox individually... what i mean is to be able to read all the combobox values without actually having to click on the combobox itself before... fore example if "Ford" is the default value, I would like to be able to read it's value without having to click on the combobox.

Many thanks.

Recommended Answers

All 6 Replies

Look at API for the class JComboBox. There is a method called: getSelectedValue or something like that. It returns an Object. You can use that to get what is selected.
If what is added in the ComboBoxes are Strings then you can cast the result of the above method to a String.

You can have one button. Once clicked read the values from all comboBoxes

There's a good tutorial for the basics here.

There's a good tutorial for the basics here.

I had checked the Sun tutorial but still stuck... i'm now trying to figure out the getSelectedValue mentioned in the first post...

I had checked the Sun tutorial but still stuck... i'm now trying to figure out the getSelectedValue mentioned in the first post...

In that tutorial there is this code:

String petName = (String)cb.getSelectedItem();

Where cb is the JComboBox. In the tutorial that piece of code is called whenever the comboBox is clicked, but you can call that method whenever you want. When you click the button call that method.

Hi all,

many thanks for all the help.

JavaAddict: I ended up using with the following (don't know if it was the best way to go about it, but it worked):

Object itemA = inComboBox01.getSelectedItem();

To be fair I have a lot of learning to do. I'm going through some books now.

Thanks all for the help.

Hi all,

many thanks for all the help.

JavaAddict: I ended up using with the following (don't know if it was the best way to go about it, but it worked):

Object itemA = inComboBox01.getSelectedItem();

To be fair I have a lot of learning to do. I'm going through some books now.

Thanks all for the help.

Actually it is the same thing. But if you wanted to do more manipulations you would need to cast the object returned to its class. Meaning that if you don't use the gui builder to add items to the combo box, you can use the add item method that takes as argument an object. Any object. Then with getSelectedItem you will have that object and all the data it has:

Try this:

class Person {
  private String name;  
  private int age;
  private String phone;

  public Person(String n, int a, String p) {
   name = n;
   age = a;
   phone = p;
  }

  // get, set methods
}

Then create an empty JComboBox and go to the code and do this:

comboBox.addItem( new Person("Name 1",10,"111111") );
comboBox.addItem( new Person("Name 2",20,"22222") );
comboBox.addItem( new Person("Name 3",30,"333333") );

And see what it prints.

Then go to the Person class and add this method:

public String toString() {
  return name;
}

And try to see again the combo box.

Whenever you want to get the selected person just do:

Person pers= (Person)comboBox.getSelectedItem();
// now you can call the getAge and getPhone methods
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.