Reputation:
To make an item I have some jComboboxes in a jform where you can select some options and when you want to edit something some values of an object (toString) are displayed in a jcombobox in another jform with the value selected. But it doesn't want to display the values in the combobox. I want to show the name + firstname in the combobox (toString)
try {
ak = pdb.seekPerson(v.getBuyerId());
coKoper.removeAllItems();
} catch (ApplicationException ae) {
javax.swing.JOptionPane.showMessageDialog(this, ae.getMessage());
}
initiateCombo(); //adds the objects tot the combo
coBuyer.setSelectedItem(ak.toString());
}
private void initiateCombo() {
PersonDB pdb = new PersonDB();
try {
ArrayList<Persons> buyer = pdb.seekAllBuyers();
for (Persons p : buyer) {
coBuyer.addItem(p);
}
}
Upvotes: 0
Views: 1010
Reputation: 5140
Well, do you try to override the toString()
method of your class Persons
?
Something like:
@Override
public String toString() {
return name + " " + firstname ;
}
Moreover, use setSelectedItem
like this (I suppose ak
is an instance of Persons
):
coBuyer.setSelectedItem(ak);
Don't forget to override the equals(Object o)
method ;)
@Override
public boolean equals(Object o) {
if( o instanceof Persons ){
boolean check = /* here your own code*/;
return check;
}
return false ;
}
Upvotes: 1
Reputation: 2588
It looks like the coBuyer combobox contains Persons objects. You retrieve the Persons object you want to select and store it in ak.
The combobox is going to display the toString() method of the Persons object. By default, this displays Object.toString(). You need to @Override toString() in the Persons class and have it return name + firstname, or whatever you want the combobox to display.
Upvotes: 0