我有转换问题;我的变量CodeA是String
类型col.setCodeA((Collabo)jComboBoxAlbum.getSelectedItem());
我的错误讯息是Object cannot be converted to String
在这种情况下如何转换我的变量CodeA?
Collabo col = new Collabo();
col.setCodeA((Collabo)jComboBoxAlbum.getSelectedItem());
boolean ok = daoCol.insertCollabo(col);
if (!ok)
JOptionPane.showMessageDialog(null,"Insertion impossible !","Avertissement",JOptionPane.ERROR_MESSAGE);
this.dispose();
这是我的课程Collabo:
public class Collabo {
private String codeA;
private Musicien mutApp;
private Instrument insApp;
public Collabo(String codeA, Musicien mutApp, Instrument insApp) {
this.codeA = codeA;
this.mutApp = mutApp;
this.insApp = insApp;
}
public String getCodeA() {
return codeA;
}
public void setCodeA(String codeA) {
this.codeA = codeA;
}
public Musicien getMutApp() {
return mutApp;
}
public void setMutApp(Musicien mutApp) {
this.mutApp = mutApp;
}
public Instrument getInsApp() {
return insApp;
}
public void setInsApp(Instrument insApp) {
this.insApp = insApp;
}
public String toString() {
return codeA;
}
答案 0 :(得分:0)
假设您的jComboBoxAlbum包含Collabo类型的对象(因为您将结果转换为该类型),并且您希望将新Collabo实例的字符串设置为与所选项目中的字符串相同,只需调用选定项目的getCodeA()方法:
Collabo col = new Collabo();
Collabo selected = (Collabo)jComboBoxAlbum.getSelectedItem();
col.setCodeA(selected.getCodeA);