Alright, I am working on a rather large program.
I will try to put in only what you need.
So right now, the comboBox
is filling (with the override toString
), and the first item is selected.
When I choose different parameters and force the contents of the comboBox
to change, the new list is put into the comboBox
and the first item is selected again.
I can see the update, so I know it is filling properly.
The problem is that when I select anything in the comboBox
, nothing happens.
The first item remains selected, none of my System.out.println
lines are printing, so nothing is being executed.
When I remove my override toString
, everything works as intended.
The strange part, past that, is when having this override toString
removed, it falls back on the parent class who has an override toString
.
What is going on?
To my understanding the toString
literally changes what is displayed, it does not change the data.
I am adding the objects to the comboBox
, but displaying a bit of the information.
public class Belt extends Part{
//variable initialization and methods
@override
public String toString(){
String display = this.getCode() + " - " + this.color;
return display;
}
public final class Something implements ActionListener{
//variable initialization and methods
//there are several methods that call the fillBeltCombo()
GridBagConstraints c = new GridBagConstraints();
private void pad(GridBagConstraints c){
c.anchor = GridBagConstraints.NORTHWEST;
c.weightx = 1;
c.insets.left = 10;
c.insets.right = 10;
c.insets.top = 5;
c.insets.bottom = 5;
}
beltCombo = new JComboBox();
beltCombo.setVisible(true);
c.gridwidth = 2;
c.gridx = 4;
c.gridy = 9;
beltCombo.addActionListener((ActionEvent eventBelt) -> {
JComboBox beltCodeCombo1 = (JComboBox) eventBelt.getSource();
if(beltCombo.getItemCount()>0){
currentProduct.setBelt((Belt)beltCodeCombo1.getSelectedItem());
}else{/*do nothing*/}
});
pane.add(beltCombo, c);
public static void fillBeltCombo(ArrayList<Belt> list){
beltCombo.removeAllItems();
int size = list.size();
for(int x=0; x<size; x++){
beltCombo.addItem(list.get(x));
}
}
}
Aright, so based on yesterdays successful creation (which I was frustrated by) I continued to fill in the skeleton until it broke again. - Adjusted classes to reflect the actual types of data that I was dealing with. - Added my override equals method <-------problem
What happened was that when I originally created the equals override, all of the toStrings were the same, and that was displayed in the comboBox. So the equals override looked like
@Override public boolean equals(Object other){
if (other == null) return false;
return (String.valueOf(other)).equals(this.description);
}
The problem with this, was that when I was asked to change what was displayed in the comboBox, I did not realize that the comboBox seems to use that equals check. The new other Object was
this.getCode() + " - " + this.color;
and it was being compared to num, which returned false.
Now correct me if I am wrong, but the moral of the story seems to be: JComboBox will not let you select something from the list that does not equal something on the list. So because my equals override changed how the objects were compared: string value of object, compared to string variable of the object- the same object was not equaling itself. The adjusted code:
@Override public boolean equals(Object other){
if (other == null) return false;
return ((Parent)other).description.equals(this.description);
}