切换屏幕时设置Combobox值

时间:2017-05-19 16:48:56

标签: javafx combobox fxml

我想在Radiobuttons之间切换FXML文件而不会丢失Combobox-Selection。 “secSelectionModel()”似乎添加了一个Selection,但它没有直观显示。

public class Controller {
    static SingleSelectionModel cbxSelection;
    @FXML private ComboBox<String> cbx = new ComboBox<>();
    @FXML private RadioButton radio1 = new RadioButton();
    @FXML private RadioButton radio2 = new RadioButton();

    @FXML private void cbxFill(){
        cbx.getItems().setAll("eins","zwei");
    }

    @FXML private void screenSwitch() throws IOException {
        Stage stage;
        Parent root;

        if(radio1.isSelected()){
            stage=(Stage) radio1.getScene().getWindow();
            root = FXMLLoader.load(getClass().getResource("sample.fxml"));
            //cbxFill();
            cbx.setSelectionModel(cbxSelection);
        }
        else{
            stage=(Stage) radio2.getScene().getWindow();
            root = FXMLLoader.load(getClass().getResource("sample2.fxml"));
            //cbxFill();
            cbx.setSelectionModel(cbxSelection);
        }
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    @FXML private void getcbxSelection(){
        cbxSelection=cbx.getSelectionModel();
    }
}

If Radio2 is selected, I want to switch to Screen2 without losing the Entered Selection in the Combobox.

0 个答案:

没有答案