Tanja
Tanja

Reputation: 41

How to display custom objects (with toString) in a JComboBox

I am rather new to Java, so please keep that in mind...

I have a swing GUI in which contains a JComboBox. I want the user to select one option of an ArrayList<Person>. Please note that I could also use an array. I have used JComboBox before, but only with Strings. The Person class has a toString() method that I want to use to get the text that should represent the object in the JComboBox.

When a JButton is pushed later, the selected Person object (the same one) should be added to another ArrayList for further operations.

I want the button to do something like this:

Person selectedPerson = (Person) myComboBox.getselectedItem();

However, I somehow need to get those Person objects into that thing somehow. I tried to use:

myComboBox.setModel(new DefaultComboBoxModel<>(aListOfStuff);

I even tried making my own model:

public class DropDownModel extends DefaultComboBoxModel<Person> implementsMutableComboBoxModel<Person>

but the setModel method wants none of that.


I have seen other kind of similar questions on Stackoverflow, but none of them answer my question. I've also noticed someone mention a renderer class or something like that.

Again: I want an ArrayList to have its Person objects selectable in a JComboBox so that I can access the selected one with Person tempPerson = (Person) myComboBox.getSelectedObject();.

Do I have to make my own model class?

I pulled off displaying them in a JList. Using a custom model:

public class ListBoxModel extends AbstractListModel<Person> implements Iterable<Person>

Do I need something simular to that?

Is this possible at all?

Some examples:

myComboBox.setModel(new DefaultComboBoxModel<>(s.getPersonListAsArray()));
cbRemoveClass.setModel(new DefaultComboBoxModel<>(t.getClasses().toArray(new String[t.getClasses().size()])));
someOtherComboBox.setModel(new DropDownModel(dlModel.getList())); // trying to use my custom model

Upvotes: 0

Views: 2438

Answers (2)

CoupFlu
CoupFlu

Reputation: 321

I know I'm late to this, but this is the answer you were looking for:

JComboBox<ProtocolInterface> protocolCombo = new javax.swing.JComboBox<>();
HashSet<ProtocolInterface> protocols=this.myDevice.getSupportedProtocols();
protocolCombo.setModel(new javax.swing.DefaultComboBoxModel<ProtocolInterface>(protocols.toArray(new ProtocolInterface[protocols.size()])));

Just make sure that your object (in this case ProtocolInterface) has a "toString" method that displays the text you want like this:

@Override
public String toString() {
    return name;
}

Upvotes: 1

camickr
camickr

Reputation: 324118

Again: I want an ArrayList to have its Person objects selectable in a JComboBox

That is not how it works. The ArrayList means nothing to the combo box. The data needs to be stored in the ComboBoxModel.

So you need to add the data from the ArrayList to the model.

You write a simple loop to iterate through the ArrayList and then you use a combo box convenience method to add an item to the model:

for (each item in the ArrayList)
    comboBox.addItem( theItem );

Upvotes: 1

Related Questions