0

我的问题如下:

class xxx {
@FXML
private Combobox<Integer> cmb_year;
...
public void method ()
{
 int year=2013;
 cmb_year.getItems().add(year);
 cmb_year.setValue(year) ---> argumenttype mismatch
}
}

这是我的代码片段,但显示了我遇到的问题。

我试过了

  • 将“int year”转换为“Integer year”
  • 访问 overcmb_year.getSelectionModel().select(new Integer(year))
  • 通过 cmb_year.getSelectionModel().select(year) 访问

总是导致参数类型不匹配。

什么会导致这种情况?

4

2 回答 2

0

如果您尝试设置选择哪个项目,您希望使用 ComboBox 的 SelectionModel:

cmb_year.getSelectionModel().select(cmb_year.getItems().indexOf(year));

你也可以试试setSelectedItem(year)selectLast()

于 2013-05-30T16:20:59.183 回答
0

这可能只是 ComboBox 未正确或完全初始化的问题。我从不使用“开箱即用”的组合框。我使用几行代码来设置它们。

以下是我的一个对话框控制器类中的 initialize() 方法的代码摘录(此 ComboBox 显示机构对象列表):

    // this first line gets the data from my data source
    // the ComboBox is referenced by the variable 'cbxInst'

    ObservableList<Institution> ilist = Institution.getInstitutionList(); 
    Callback<ListView<Institution>, ListCell<Institution>> cellfactory =
            new Callback<ListView<Institution>, ListCell<Institution>>() {
                @Override
                public ListCell<Institution> call(ListView<Institution> p) {
                    return new InstitutionListCell();
                }
            };

    cbxInst.setCellFactory(cellfactory);
    cbxInst.setButtonCell(cellfactory.call(null));
    cbxInst.setItems(ilist);

这里的关键点是:

  • 我定义了一个单元工厂来生成 ListCell 实例以供 ComboBox 显示。

  • 我使用工厂创建一个 ListCell 实例来初始化按钮单元。

为了完整起见,这里是创建机构 ListCell 实例的私有成员类:

private final class InstitutionListCell extends ListCell<Institution> {

    @Override
    protected void updateItem(Institution item, boolean empty){
        super.updateItem(item, empty);

        if (item != null) {                
            this.setText(item.getName());

        } else {
            this.setText(Census.FORMAT_TEXT_NULL);
        }
    }
}

如果您以类似的方式初始化您的 ComboBox,那么您的问题可能会得到解决。

于 2013-05-30T17:02:35.303 回答