JAVAFX:在BorderPane中切换面板

huangapple go评论123阅读模式
英文:

JAVAFX: Switching Between Panes in BorderPane

问题

我有一个桌面应用程序,包含以下内容:

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

SideBar.fxml:包含BorderPane -> 在其左侧,我创建了2个按钮:

  1. - 第一个按钮:加载示例FXML文件
  2. - 第二个按钮:加载secondFxml文件

sample.fxml:包含一个tableView和一个Button
secondFxml.fxml:包含一个标签

控制器:控制sample.fxml -> 将随机双精度值加载到tableView中

问题是:

当我按下Pane 1中的按钮(填充表格)时,它将数据加载到tableView中,到目前为止一切都进行得很好。

当我切换到第二个窗格,然后返回到第一个窗格时,中央边框窗格会重新加载,因此tableView的数据消失了。

我想要的是,当我返回到第一个窗格时,tableView保持原样,不会重新加载。

我尝试隐藏borderpane的中央部分,但对我没有效果。

我已经对问题进行了截图:JAVAFX:在BorderPane中切换面板

Main类:

  1. public class Main extends Application {
  2. @Override
  3. public void start(Stage primaryStage) throws Exception{
  4. Parent root = FXMLLoader.load(getClass().getResource("SideBar.fxml"));
  5. primaryStage.setTitle("Hello World");
  6. primaryStage.setScene(new Scene(root, 700, 500));
  7. primaryStage.show();
  8. }
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. }

SideBarController类:

  1. public class SideBarController implements Initializable {
  2. @FXML BorderPane borderPane;
  3. public void openPane1(ActionEvent event) throws Exception {
  4. loadScene("Sample.fxml");
  5. }
  6. public void openPane2(ActionEvent event) throws Exception {
  7. loadScene("secondFxml.fxml");
  8. }
  9. private void loadScene(String sc) throws IOException {
  10. Parent root = FXMLLoader.load(getClass().getResource(sc));
  11. borderPane.setCenter(root);
  12. }
  13. @Override
  14. public void initialize(URL location, ResourceBundle resources) { }
  15. }

Controller类:

  1. public class Controller implements Initializable {
  2. double[][] data = new double[5][5];
  3. Random random = new Random();
  4. ObservableList<double[]> observableLists = FXCollections.observableArrayList();
  5. @FXML
  6. TableView<double[]> tableView = new TableView<>(observableLists);
  7. @FXML
  8. public void fillTable(ActionEvent event) throws IOException {
  9. //Random Values
  10. for (int i = 0; i < data.length ; i++) {
  11. for (int j = 0; j < data[0].length ; j++) {
  12. data[i][j]= random.nextDouble();
  13. }
  14. }
  15. //Add data to ObservableLists
  16. for (int i = 0; i < data.length ; i++) {
  17. observableLists.add(data[i]);
  18. }
  19. //Create Columns
  20. for (int i = 0; i < data[0].length ; i++) {
  21. TableColumn<double[], Double> column= null;
  22. column = new TableColumn<>("column "+i);
  23. int finalI = i;
  24. column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()[finalI]));
  25. tableView.getColumns().add(column);
  26. }
  27. // Fill TableView
  28. tableView.setItems(observableLists);
  29. }
  30. @Override
  31. public void initialize(URL location, ResourceBundle resources) {
  32. }
  33. }

SideBar.fxml:

  1. <BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SideBarController">
  2. <left>
  3. <VBox prefHeight="400.0" prefWidth="173.0" style="-fx-background-color: black;" BorderPane.alignment="CENTER">
  4. <children>
  5. <Button mnemonicParsing="false" onAction="#openPane1" prefHeight="25.0" prefWidth="177.0" style="-fx-background-color: blue; -fx-border-color: white;" text="Pane 1" textFill="WHITE">
  6. <VBox.margin>
  7. <Insets top="50.0" />
  8. </VBox.margin>
  9. <font>
  10. <Font name="System Bold" size="17.0" />
  11. </font>
  12. </Button>
  13. <Button mnemonicParsing="false" onAction="#openPane2" prefHeight="25.0" prefWidth="176.0" style="-fx-background-color: blue; -fx-border-color: white;" text="Pane 2" textFill="WHITE">
  14. <VBox.margin>
  15. <Insets top="10.0" />
  16. </VBox.margin>
  17. <font>
  18. <Font name="System Bold" size="17.0" />
  19. </font>
  20. </Button>
  21. </children>
  22. </VBox>
  23. </left>
  24. <center>
  25. <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
  26. <children>
  27. <Label layoutX="163.0" layoutY="152.0" prefHeight="68.0" prefWidth="131.0" text="Home">
  28. <font>
  29. <Font size="46.0" />
  30. </font>
  31. </Label>
  32. </children>
  33. </Pane>
  34. </center>
  35. </BorderPane>

Sample.fxml:

  1. <Pane prefHeight="395.0" prefWidth="597.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
  2. <children>
  3. <TableView fx:id="tableView" layoutX="77.0" layoutY="47.0" prefHeight="266.0" prefWidth="461.0" />
  4. <Button layoutX="257.0" layoutY="329.0" mnemonicParsing="false" onAction="#fillTable" text="fill Table" />
  5. </children>
  6. </Pane>

SecondFxml.fxml:

  1. <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
  2. <children>
  3. <Label layoutX="232.0" layoutY="153.0" text="Pane 2">
  4. <font>
  5. <Font size="46.0" />
  6. </font>
  7. </Label>
  8. </children>
  9. </Pane>
英文:

I have a desktop app that contains:

Main Class: that load the first fxml file -> SideBar.fxml

SideBar.fxml: contains BorderPane -> at the left of it, i create 2 buttons:

  1. - the fist button: load sample fxml file
  2. - the second button: load secondFxml file

sample.fxml: contains a tableView and a Button
secondFxml.fxml: contains a label

Controller: a class that control sample.fxml -> load random double values to a tableView

the issue is :

when i press the button (fill Table) in Pane 1 : it load the data to the tableView, untill now everything is going well

when i switch to the second pane and i return to the first pane the center border pane is reloaded again so the data of the tableView disappeared

what i want is when i return to the first pane the the table view stay as it was first
i try to hide the borderpane center but it doesn't work for me

i screenShot the issue: JAVAFX:在BorderPane中切换面板

Main:

  1. public class Main extends Application {
  2. @Override
  3. public void start(Stage primaryStage) throws Exception{
  4. Parent root = FXMLLoader.load(getClass().getResource(&quot;SideBar.fxml&quot;));
  5. primaryStage.setTitle(&quot;Hello World&quot;);
  6. primaryStage.setScene(new Scene(root, 700, 500));
  7. primaryStage.show();
  8. }
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. }

SideBarController:

  1. public class SideBarController implements Initializable {
  2. @FXML BorderPane borderPane;
  3. public void openPane1(ActionEvent event) throws Exception {
  4. loadScene(&quot;Sample.fxml&quot;);
  5. }
  6. public void openPane2(ActionEvent event) throws Exception {
  7. loadScene(&quot;secondFxml.fxml&quot;);
  8. }
  9. private void loadScene(String sc) throws IOException {
  10. Parent root = FXMLLoader.load(getClass().getResource(sc));
  11. borderPane.setCenter(root);
  12. }
  13. @Override
  14. public void initialize(URL location, ResourceBundle resources) { }
  15. }

Controller:

  1. public class Controller implements Initializable {
  2. double[][] data = new double[5][5];
  3. Random random = new Random();
  4. ObservableList&lt;double[]&gt; observableLists = FXCollections.observableArrayList();
  5. @FXML
  6. TableView&lt;double []&gt; tableView = new TableView&lt;&gt;(observableLists);
  7. @FXML
  8. public void fillTable(ActionEvent event) throws IOException {
  9. //Random Values
  10. for (int i = 0; i &lt;data.length ; i++) {
  11. for (int j = 0; j &lt;data[0].length ; j++) {
  12. data[i][j]= random.nextDouble();
  13. }
  14. }
  15. //Add data to ObservableLists
  16. for (int i = 0; i &lt;data.length ; i++) {
  17. observableLists.add(data[i]);
  18. }
  19. //Create Columns
  20. for (int i = 0; i &lt;data[0].length ; i++) {
  21. TableColumn&lt;double[], Double&gt; column= null;
  22. column = new TableColumn&lt;&gt;(&quot;column &quot;+i);
  23. int finalI = i;
  24. column.setCellValueFactory(param -&gt; new ReadOnlyObjectWrapper&lt;&gt;(param.getValue()[finalI]));
  25. tableView.getColumns().add(column);
  26. }
  27. // Fill TableView
  28. tableView.setItems(observableLists);
  29. }
  30. @Override
  31. public void initialize(URL location, ResourceBundle resources) {
  32. }
  33. }

SideBar.fxml

  1. &lt;BorderPane fx:id=&quot;borderPane&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;sample.SideBarController&quot;&gt;
  2. &lt;left&gt;
  3. &lt;VBox prefHeight=&quot;400.0&quot; prefWidth=&quot;173.0&quot; style=&quot;-fx-background-color: black;&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
  4. &lt;children&gt;
  5. &lt;Button mnemonicParsing=&quot;false&quot; onAction=&quot;#openPane1&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;177.0&quot; style=&quot;-fx-background-color: blue; -fx-border-color: white;&quot; text=&quot;Pane 1&quot; textFill=&quot;WHITE&quot;&gt;
  6. &lt;VBox.margin&gt;
  7. &lt;Insets top=&quot;50.0&quot; /&gt;
  8. &lt;/VBox.margin&gt;
  9. &lt;font&gt;
  10. &lt;Font name=&quot;System Bold&quot; size=&quot;17.0&quot; /&gt;
  11. &lt;/font&gt;
  12. &lt;/Button&gt;
  13. &lt;Button mnemonicParsing=&quot;false&quot; onAction=&quot;#openPane2&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;176.0&quot; style=&quot;-fx-background-color: blue; -fx-border-color: white;&quot; text=&quot;Pane 2&quot; textFill=&quot;WHITE&quot;&gt;
  14. &lt;VBox.margin&gt;
  15. &lt;Insets top=&quot;10.0&quot; /&gt;
  16. &lt;/VBox.margin&gt;
  17. &lt;font&gt;
  18. &lt;Font name=&quot;System Bold&quot; size=&quot;17.0&quot; /&gt;
  19. &lt;/font&gt;
  20. &lt;/Button&gt;
  21. &lt;/children&gt;
  22. &lt;/VBox&gt;
  23. &lt;/left&gt;
  24. &lt;center&gt;
  25. &lt;Pane prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
  26. &lt;children&gt;
  27. &lt;Label layoutX=&quot;163.0&quot; layoutY=&quot;152.0&quot; prefHeight=&quot;68.0&quot; prefWidth=&quot;131.0&quot; text=&quot;Home&quot;&gt;
  28. &lt;font&gt;
  29. &lt;Font size=&quot;46.0&quot; /&gt;
  30. &lt;/font&gt;
  31. &lt;/Label&gt;
  32. &lt;/children&gt;
  33. &lt;/Pane&gt;
  34. &lt;/center&gt;
  35. &lt;/BorderPane&gt;

Sample.fxml

  1. &lt;Pane prefHeight=&quot;395.0&quot; prefWidth=&quot;597.0&quot; style=&quot;-fx-background-color: white;&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;sample.Controller&quot;&gt;
  2. &lt;children&gt;
  3. &lt;TableView fx:id=&quot;tableView&quot; layoutX=&quot;77.0&quot; layoutY=&quot;47.0&quot; prefHeight=&quot;266.0&quot; prefWidth=&quot;461.0&quot; /&gt;
  4. &lt;Button layoutX=&quot;257.0&quot; layoutY=&quot;329.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#fillTable&quot; text=&quot;fill Table&quot; /&gt;
  5. &lt;/children&gt;
  6. &lt;/Pane&gt;

SecondFxml.fxml

  1. &lt;Pane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
  2. &lt;children&gt;
  3. &lt;Label layoutX=&quot;232.0&quot; layoutY=&quot;153.0&quot; text=&quot;Pane 2&quot;&gt;
  4. &lt;font&gt;
  5. &lt;Font size=&quot;46.0&quot; /&gt;
  6. &lt;/font&gt;
  7. &lt;/Label&gt;
  8. &lt;/children&gt;
  9. &lt;/Pane&gt;

答案1

得分: 1

不要在按钮点击时从FXML重新加载。请在initialize方法中执行一次:

  1. public class SideBarController implements Initializable {
  2. @FXML BorderPane borderPane;
  3. private Parent sample, secondFxml;
  4. public void openPane1(ActionEvent event) throws Exception {
  5. borderPane.setCenter(sample);
  6. }
  7. public void openPane2(ActionEvent event) throws Exception {
  8. borderPane.setCenter(secondFxml);
  9. }
  10. private Parent loadScene(String sc) throws IOException {
  11. return FXMLLoader.load(getClass().getResource(sc));
  12. }
  13. @Override
  14. public void initialize(URL location, ResourceBundle resources) {
  15. try {
  16. sample = loadScene("Sample.fxml");
  17. secondFxml = loadScene("secondFxml.fxml");
  18. } catch (IOException ex) {
  19. ex.printStackTrace();
  20. };
  21. }
  22. }
英文:

Do not reload from fxml when button is clicked. Do it once in initialize:

  1. public class SideBarController implements Initializable {
  2. @FXML BorderPane borderPane;
  3. private Parent sample, secondFxml;
  4. public void openPane1(ActionEvent event) throws Exception {
  5. borderPane.setCenter(sample);
  6. }
  7. public void openPane2(ActionEvent event) throws Exception {
  8. borderPane.setCenter(secondFxml);
  9. }
  10. private Parent loadScene(String sc) throws IOException {
  11. return FXMLLoader.load(getClass().getResource(sc));
  12. }
  13. @Override
  14. public void initialize(URL location, ResourceBundle resources) {
  15. try {
  16. sample = loadScene(&quot;Sample.fxml&quot;);
  17. secondFxml = loadScene(&quot;secondFxml.fxml&quot;);
  18. } catch (IOException ex) {
  19. ex.printStackTrace();
  20. };
  21. }
  22. }

huangapple
  • 本文由 发表于 2020年4月4日 04:51:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61020215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定