简体   繁体   English

javafx组合框 <Integer> 类型不匹配

[英]javafx Combobox<Integer> type mismatch

my problem is as follows: 我的问题如下:

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
}
}

It's fragments of my code, but shows the problem I encounter. 它是我的代码的片段,但显示了我遇到的问题。

I've tried with 我尝试过

  • making "int year" to "Integer year" 使“整数年”变为“整数年”
  • Accessing overcmb_year.getSelectionModel().select(new Integer(year)) 访问overcmb_year.getSelectionModel()。select(新的整数(年))
  • Accessing over cmb_year.getSelectionModel().select(year) 通过cmb_year.getSelectionModel()。select(year)访问

Always leads to an argumenttype mismatch. 总是导致参数类型不匹配。

What can cause this? 是什么原因造成的?

If you are trying to set which item is selected, you want to work with the ComboBox's SelectionModel: 如果要设置选择的项目,则要使用ComboBox的SelectionModel:

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

You could also try setSelectedItem(year) or selectLast() . 您也可以尝试setSelectedItem(year)selectLast()

This may simply be an issue with the ComboBox not being initialized properly or completely. 这可能是ComboBox没有正确或完全初始化的问题。 I never use ComboBoxes "out of the box". 我从不“开箱即用”使用ComboBoxes。 I use several lines of code to set them up. 我使用几行代码进行设置。

Following is a code excerpt from the initialize() method in one of my dialog controller classes (this ComboBox displays a list of Institution objects): 以下是我的一个对话框控制器类中的initialize()方法的代码摘录(此ComboBox显示Institution对象的列表):

    // 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);

The key points here are: 这里的关键点是:

  • I define a cell factory to generate ListCell instances for the ComboBox to display. 我定义了一个单元工厂,以生成ListCell实例供ComboBox显示。

  • I use the factory to create a ListCell instance to initialize the Button Cell with. 我使用工厂创建一个ListCell实例来初始化Button Cell。

For completeness, here is the private member class from which the Institution ListCell instances are created: 为了完整起见,这是用于创建Institution 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);
        }
    }
}

If you were to initialize your ComboBox in a similar way, it may be that your problems will be solved. 如果以类似的方式初始化ComboBox,则可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM