如何正确实现可观察列表以填充组合框

时间:2019-06-28 18:32:25

标签: java javafx javafx-8 fxml observablelist

我目前正在制作一个将用作日历的项目。目前,我正在尝试实现一个组合框,该组合框将允许用户选择要跳过的月份。我在执行可观察列表时遇到了麻烦。我看过其他教程,但似乎仍然无法真正使组合框选择列表中的内容。我的observableArrayList函数参数不正确吗? https://github.com/zhadjah9559/Side_Proj_Calendar.git

public class FXMLDocumentController implements Initializable {


@FXML
private Pane top, bottom, leftPane;

@FXML
private GridPane CalendarPane;

@FXML
private Label monthTitle, eventsListLabel, createEventsLabel, createdEventsLabels;

@FXML 
private TextArea bottomTextArea;

@FXML
private DatePicker datePicker;



//Creating an observable list to populate comboBox
private ObservableList<String> list = FXCollections.observableArrayList("month"); //only testing

public ObservableList<String> getList() {
    return list;
}

public void setList(ObservableList<String> list) {
    this.list = list;
}

//populating the comboBox
@FXML
private ComboBox chooseMonth;




// individual dates or days (monday - sunday)
@FXML
private Label Label00, Label02, Label03, Label04,Label05,Label10,
        Label11, Label12, Label13, Label14, Label15, Label20, Label21,
        Label22, Label23, Label24, Label25, Label30, Label31, Label32,
        Label33, Label34, Label35, Label40, Label41, Label42, Label43,
        Label44, Label45, Label50, Label51, Label52, Label53, Label54,
        Label55, Label60, Label61, Label62, Label63, Label64, Label65;

@Override
public void initialize(URL url, ResourceBundle rb) {
    chooseMonth.setItems(list);
}    

@FXML
private void loadFebruary(ActionEvent event) throws IOException {   
    Parent registerPane = FXMLLoader.load(getClass().getResource("February.fxml"));
    Scene ViewScene = new Scene(registerPane);

    Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
    window.setScene(ViewScene);
    window.show();
} 

}

2 个答案:

答案 0 :(得分:2)

您的代码甚至无法按书面形式进行编译。您正在尝试将String传递给FXCollections.observableArrayList()方法。该方法不接受String参数。

要创建适当的ObservableList,您需要将实际的List传递给方法。这是一个这样的示例,使用Arrays.asList()即时生成List

ObservableList<String> list = FXCollections.observableList(Arrays.asList("Jan", "Feb", "Mar"));

您可以在此处设置ComboBox项:

comboBox.setItems(list);

答案 1 :(得分:0)

我所做的与Zephyr建议的类似,但略有不同。主要区别在于最后,允许在组合框中设置默认值。

在我的项目中,正在根据组合框中当前选择的值填充信息的表视图。也许这是用户错误,但默认情况下,启动时我的组合框为空白,因此程序读取错误且无法正常运行。因此,在组合框中添加默认值解决了我的问题。

将此代码放置在FXML项(例如按钮,文本字段等)之后,以创建列表:

private final ObservableList<String> allMonths = FXCollections.observableArrayList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

并在您的initialize方法下,使用以下命令填充该列表:

monthComboBox.setItems(allMonths);

您还可以通过将列表中的一项(即“一月”)设置为默认选项,并在列表中放置以下内容来将其更进一步:

monthComboBox.getSelectionModel().selectFirst();

总而言之,FXMLController看起来像这样,假设上面已正确导入(因为我不记得所有输入都放在我的头上):

public class FXMLDocumentController implements Initializable {

    @FXML private ComboBox<String> monthComboBox;

    private final ObservableList<String> allMonths = FXCollections.observableArrayList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    monthComboBox.setItems(allMonths);
    monthComboBox.getSelectionModel().selectFirst();
    }
}