这个问题类似于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'。我知道解决方案离我很近,所以请建议我应该做些什么..
答案 0 :(得分:0)
可以试试THIS LINK。他们在这里解释:
基本上他们会解释你需要知道的一切。
祝你好运:)祝你好运, Dimitar Georgiev
答案 1 :(得分:0)
正确的解决方案是在initialize()方法中添加ChoiceBox监听器,如下所示:
private void getLastTenObjects()
{
foreach (ChartObjectsInt chartRecords in arraylistMonitor)
{
for (int i = 0; i < 10; i++)
{
arraylistMonitor.IndexOf(i);
}
}
}