JAVAFX:在BorderPane中的窗格之间切换

JAVAFX:在BorderPane中的窗格之间切换,java,javafx,tableview,borderpane,Java,Javafx,Tableview,Borderpane,我有一个桌面应用程序,其中包含: 主类:加载第一个fxml文件->SideBar.fxml SideBar.fxml:包含边框窗格->在其左侧,我创建了两个按钮: - the fist button: load sample fxml file - the second button: load secondFxml file sample.fxml:包含一个tableView和一个按钮 secondFxml.fxml:包含一个标签 控制器:控制sampl

我有一个桌面应用程序,其中包含:

主类:加载第一个fxml文件->SideBar.fxml

SideBar.fxml:包含边框窗格->在其左侧,我创建了两个按钮:

         - the fist button: load  sample fxml file
         - the second button: load secondFxml file
sample.fxml:包含一个tableView和一个按钮 secondFxml.fxml:包含一个标签

控制器:控制sample.fxml->将随机双值加载到tableView的类

问题是:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("SideBar.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 700, 500));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
public class SideBarController implements Initializable {

    @FXML BorderPane borderPane;

    public void openPane1(ActionEvent event) throws Exception {
        loadScene("Sample.fxml");
    }

    public void openPane2(ActionEvent event) throws Exception {
        loadScene("secondFxml.fxml");
    }

    private void loadScene(String sc) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource(sc));

        borderPane.setCenter(root);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) { }
}

public class Controller implements Initializable {

    double[][] data = new double[5][5];
    Random random = new Random();

    ObservableList<double[]> observableLists = FXCollections.observableArrayList();

    @FXML
    TableView<double []> tableView = new TableView<>(observableLists);

    @FXML
    public void fillTable(ActionEvent event) throws IOException {
        //Random Values
        for (int i = 0; i <data.length ; i++) {
            for (int j = 0; j <data[0].length ; j++) {
                data[i][j]= random.nextDouble();
            }
        }

        //Add data to ObservableLists
        for (int i = 0; i <data.length ; i++) {
            observableLists.add(data[i]);
        }

        //Create Columns
        for (int i = 0; i <data[0].length ; i++) {
            TableColumn<double[], Double> column= null;
            column = new TableColumn<>("column "+i);
            int finalI = i;
            column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()[finalI]));
            tableView.getColumns().add(column);
        }

        // Fill TableView
        tableView.setItems(observableLists);

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
当我按下窗格1中的按钮(填充表格):它将数据加载到tableView,直到现在一切都很顺利

当我切换到第二个窗格并返回到第一个窗格时,将再次重新加载中心边框窗格,以便tableView的数据消失

我想要的是,当我返回到第一个窗格时,表视图保持原样 我试图隐藏边框窗格中心,但它对我无效

我截图了这个问题:

Main:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("SideBar.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 700, 500));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
public class SideBarController implements Initializable {

    @FXML BorderPane borderPane;

    public void openPane1(ActionEvent event) throws Exception {
        loadScene("Sample.fxml");
    }

    public void openPane2(ActionEvent event) throws Exception {
        loadScene("secondFxml.fxml");
    }

    private void loadScene(String sc) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource(sc));

        borderPane.setCenter(root);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) { }
}

public class Controller implements Initializable {

    double[][] data = new double[5][5];
    Random random = new Random();

    ObservableList<double[]> observableLists = FXCollections.observableArrayList();

    @FXML
    TableView<double []> tableView = new TableView<>(observableLists);

    @FXML
    public void fillTable(ActionEvent event) throws IOException {
        //Random Values
        for (int i = 0; i <data.length ; i++) {
            for (int j = 0; j <data[0].length ; j++) {
                data[i][j]= random.nextDouble();
            }
        }

        //Add data to ObservableLists
        for (int i = 0; i <data.length ; i++) {
            observableLists.add(data[i]);
        }

        //Create Columns
        for (int i = 0; i <data[0].length ; i++) {
            TableColumn<double[], Double> column= null;
            column = new TableColumn<>("column "+i);
            int finalI = i;
            column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()[finalI]));
            tableView.getColumns().add(column);
        }

        // Fill TableView
        tableView.setItems(observableLists);

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
侧边栏控制器:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("SideBar.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 700, 500));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
public class SideBarController implements Initializable {

    @FXML BorderPane borderPane;

    public void openPane1(ActionEvent event) throws Exception {
        loadScene("Sample.fxml");
    }

    public void openPane2(ActionEvent event) throws Exception {
        loadScene("secondFxml.fxml");
    }

    private void loadScene(String sc) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource(sc));

        borderPane.setCenter(root);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) { }
}

public class Controller implements Initializable {

    double[][] data = new double[5][5];
    Random random = new Random();

    ObservableList<double[]> observableLists = FXCollections.observableArrayList();

    @FXML
    TableView<double []> tableView = new TableView<>(observableLists);

    @FXML
    public void fillTable(ActionEvent event) throws IOException {
        //Random Values
        for (int i = 0; i <data.length ; i++) {
            for (int j = 0; j <data[0].length ; j++) {
                data[i][j]= random.nextDouble();
            }
        }

        //Add data to ObservableLists
        for (int i = 0; i <data.length ; i++) {
            observableLists.add(data[i]);
        }

        //Create Columns
        for (int i = 0; i <data[0].length ; i++) {
            TableColumn<double[], Double> column= null;
            column = new TableColumn<>("column "+i);
            int finalI = i;
            column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()[finalI]));
            tableView.getColumns().add(column);
        }

        // Fill TableView
        tableView.setItems(observableLists);

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
控制器:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("SideBar.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 700, 500));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
public class SideBarController implements Initializable {

    @FXML BorderPane borderPane;

    public void openPane1(ActionEvent event) throws Exception {
        loadScene("Sample.fxml");
    }

    public void openPane2(ActionEvent event) throws Exception {
        loadScene("secondFxml.fxml");
    }

    private void loadScene(String sc) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource(sc));

        borderPane.setCenter(root);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) { }
}

public class Controller implements Initializable {

    double[][] data = new double[5][5];
    Random random = new Random();

    ObservableList<double[]> observableLists = FXCollections.observableArrayList();

    @FXML
    TableView<double []> tableView = new TableView<>(observableLists);

    @FXML
    public void fillTable(ActionEvent event) throws IOException {
        //Random Values
        for (int i = 0; i <data.length ; i++) {
            for (int j = 0; j <data[0].length ; j++) {
                data[i][j]= random.nextDouble();
            }
        }

        //Add data to ObservableLists
        for (int i = 0; i <data.length ; i++) {
            observableLists.add(data[i]);
        }

        //Create Columns
        for (int i = 0; i <data[0].length ; i++) {
            TableColumn<double[], Double> column= null;
            column = new TableColumn<>("column "+i);
            int finalI = i;
            column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()[finalI]));
            tableView.getColumns().add(column);
        }

        // Fill TableView
        tableView.setItems(observableLists);

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
公共类控制器实现可初始化{
双精度[]数据=新双精度[5][5];
随机=新随机();
ObservableList ObservableList=FXCollections.observableArrayList();
@FXML
TableView TableView=新的TableView(可观察列表);
@FXML
公共void fillTable(ActionEvent事件)引发IOException{
//随机值

对于(int i=0;i单击按钮时不要从fxml重新加载。在
初始化中执行一次:

public class SideBarController implements Initializable {

    @FXML BorderPane borderPane;
    private Parent sample, secondFxml;

    public void openPane1(ActionEvent event) throws Exception {
        borderPane.setCenter(sample);
    }

    public void openPane2(ActionEvent event) throws Exception {
        borderPane.setCenter(secondFxml);
    }

    private Parent loadScene(String sc) throws IOException {
        return FXMLLoader.load(getClass().getResource(sc));
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            sample = loadScene("Sample.fxml");
            secondFxml =  loadScene("secondFxml.fxml");
        } catch (IOException ex) {
            ex.printStackTrace();
        };
    }
}

这回答了你的问题吗?你有两个选择。或者。我建议第一个。@Sedrick我想在按下“窗格2”按钮时隐藏边框窗格(窗格1)的中心,并在按下“窗格1”时显示为避免重新加载窗格1导致显示的数据丢失,您的建议与我的问题无关。我的建议与您的问题非常相关。如果您了解我发布的其中一个想法,您将不必找到解决此问题的方法。