【发布时间】:2017-09-21 08:30:36
【问题描述】:
这个问题与 JavaFX 中关于 ChoiceBox 的其他问题类似,但我在代码中做了其他事情。 我有以下元素的类:
package application;
public class ControllerRegister extends Main implements Initializable {
@FXML
private ChoiceBox<String> cbSuppliers;
private ArrayList<String> suppliers = new ArrayList<String>();
public ControllerRegister() throws SQLException {/*....*/}
@FXML //controller method
void buttonOnAction2(ActionEvent event2) throws IOException, SQLException {
//push some button and then get value of choicebox
setStrTabDat()
/*....*/
}
public static void setStrTabDat(){ //my attempts to show selected value from ChoiceBox
ObservableList<String> outputOl = FXCollections.observableArrayList();
ChoiceBox<String> cbSuppliers = new ChoiceBox<String>();
outputOl = cbSuppliers.getItems();
outputOl.addAll(cbSuppliers.getValue());
String output = cbSuppliers.getValue();
System.out.println("output string: "+output);
System.out.println("output by method: " +cbSuppliers.getItems());
System.out.println("output observList: "+outputOl.toString());
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
ObservableList<String> list = FXCollections.observableArrayList(suppliers);
cbSuppliers.setItems(list);
cbSuppliers.getSelectionModel().selectFirst();
}
}
正如您在 setStrTabDat() 方法中看到的那样,我尝试从 ChoiceBox 打印选定的值,但所有三个 Sytem.out.prints 的输出都是“null”。我知道解决方案离我很近,所以请建议我该怎么做..
【问题讨论】:
标签: java javafx combobox jcombobox observablelist