对象的ComboBox忽略toString方法

时间:2013-01-05 04:36:10

标签: swing jcombobox tostring

大家好我遇到了这样的麻烦Object name insted of caption

在对话框w /组合框

    public JComboBox<IngrMeasureUnit> getUnits(){
     JComboBox<IngrMeasureUnit> result = new JComboBox<IngrMeasureUnit>();
        for ( int i = 0; i < parent.getIngrList().imul.getSize(); i++ ) {
            result.addItem(parent.getIngrList().imul.getMeasureUnit(i));
        }        
     return result;
}

和班级

public class IngrMeasureUnit {

private int id;
private String name;
private boolean mustInt;

public IngrMeasureUnit( int id, String name, boolean mustInt ) {
    this.id = id;
    this.name = name;
    this.mustInt = mustInt;
}

public String toSrting() {
    return name;
} 

无法理解这种行为,因为在其他情况下它起作用。 试图放置@Override注释,编译器拒绝了它。 感谢。

1 个答案:

答案 0 :(得分:1)

您的方法名为toSrting(),而不是toString()。当您打算覆盖方法时,始终使用@Override注释。这样,如果不这样做,编译器将发出错误:

@Override
public String toSrting() { // compiler error, because toSrting() doesn't override anything

试图放置@Override注释,编译器拒绝了它。

编译器正是拒绝它,因为该方法不会覆盖任何内容。错误意味着:您想要覆盖一个方法,但是您没有。出了点问题。在这种情况下,您应该修复方法签名,而不是删除注释。