我需要测试所选项的值来调用不同的方法,所以我写这段代码添加一个监听器,但代码生成语法错误
@FXML
private JFXComboBox<String> cmbComp;
cmbComp.valueProperty().addListener(new ChangeListener<String>() {
public void changed(ObservableValue<String> composant, String oldValue, String newValue) throws SQLException {
if(/*test item value*/){
/*do something*/
}else{
/*do other thing*/
}
}
});
Run Code Online (Sandbox Code Playgroud)
我也不需要旧值和新值,只需测试选定的值,我怎么能传递参数?
我发现错误,这里是新代码,我希望它能帮助别人
cmbComp.getSelectionModel().selectedItemProperty().addListener( (options, oldValue, newValue) -> {
System.out.println(newValue)
}
);
Run Code Online (Sandbox Code Playgroud)
rai*_*ner 13
一种更直接并避免一些额外代码行的解决方案是向组合框添加一个动作侦听器(理想情况下来自场景构建器),如下所示:
private void comboAction(ActionEvent event) {
System.out.println(comboBox_DbTables.getValue());
}
Run Code Online (Sandbox Code Playgroud)
小智 8
万一有人错过了,OP 在帖子中回答:
我发现了错误,这是新代码,希望对其他人有帮助
cmbComp.getSelectionModel().selectedItemProperty().addListener((options, oldValue, newValue) -> {
System.out.println(newValue);
});
Run Code Online (Sandbox Code Playgroud)