修改
我的一些项目在我的JComboBox上设置一个对象时遇到问题。在我的JPanel,名为ApplicationPanel,我做了这个:
...
if(application!=null){
this.formView.setApplication(application);
}
this.formView.setPositions(positions);
this.formView.setControls(controls);
this.formView.displayControl(); //I put it here because even application is null, by default it shows all Control available when formView is shown.
this.formView.setVisible(true);
在我的JPanel中调用FormView,我创建了这个代码,用于将我的所有Class / Object命名为 Control 到JComboBox中:
public final void displayControl(){
this.cmboControls.removeAllItems();
this.cmboControls.setModel(new DefaultComboBoxModel(this.controls.toArray()));
}
也是这个:
public void setApplication(Application application){
this.application = application;
Control ctrl = findControl(application);
System.out.println("You look for "+ctrl);
this.cmboControls.setSelectedItem(findControl(application));
...
这是我的findControl代码
private Control findControl(Candidate c){
Control jc = null;
System.out.println("From Candidate: control_id "+c.getPosition().getId());
for(Control ctrl: this.controls){
if(Objects.equals(ctrl.getId(), c.getPosition().getId())){
System.out.println("Found Item nUm "+ctrl.getId());
jc = ctrl;
break;
}
}
return jc;
}
控制类:
public class Control {
private Integer id;
private String controlCode;
private Position position;
//Setter and Getter Plus toString()
...
我也在stackoverflow中搜索类似的问题,但它也没有解决我的问题。我尝试将 JComboBox类型参数从字符串更改为对象,但没有任何反应。
希望有人能指出错误发生的位置。