如何在JavaFx中更改视图的一部分

时间:2017-07-19 11:06:02

标签: javafx javafx-8

我有一个观点(SplitPane水平分离)。因此,让我们将这两个部分命名为lowerAnchorPane和upperAnchorPane。在upperUnchorPane我有一个按钮。按下该按钮后我想要应用程序将lowerAnchorPane切换为在FXML文件中创建的另一个AnchorPane。我想在函数showPatients()中实现这个,但它似乎不起作用,你能建议我任何解决方案吗?我不知道加载fxml文件的任何其他方法

public class MainController {

    private Patient model;
    @FXML
    private Button newPatientButton;
    @FXML
    private Button newVisitButton;
    @FXML
    private Button showVisitsButton;
    @FXML
    private Button showPatientsButton;
    @FXML
    private AnchorPane lowerAnchorPane;

    public void initData(Patient model)
    {
        this.model = model;
    }


    @FXML
    void showPatients(ActionEvent event) throws IOException { // when button pressed
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/ListOfPatients.fxml"));
        lowerAnchorPane = loader.load();

    }

1 个答案:

答案 0 :(得分:0)

您可以注入SplitPane并将现有的下方锚点窗格替换为您加载的窗格

@FXML
private SplitPane splitPane ; // with the corresponding fx:id in the fxml file

@FXML
void showPatients(ActionEvent event) throws IOException { // when button pressed
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/ListOfPatients.fxml"));
    lowerAnchorPane = loader.load();

    // assuming the existing pane is the second one in the split pane:
    splitPane.getItems().set(1, lowerAnchorPane);
}