Как подгрузить fxml - Java

Узнай цену своей работы

Формулировка задачи:

Всем привет. Народ подскажите как реализовать данный замысел: Что должно быть: есть главный fxml в нем есть таблица и хочу в ячейку вставить другой fxml, но перед вставкой необходимо в него внести данные для отображения. Заранее благодарен. Вот пример: Simple вставить в head: Simple.fxml:
Листинг программы
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.Button?>
  3. <?import javafx.scene.control.TextArea?>
  4. <?import javafx.scene.layout.Pane?>
  5. <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="121.0" prefWidth="282.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
  6. <children>
  7. <Button fx:id="btn" layoutX="208.0" layoutY="18.0" mnemonicParsing="false" prefHeight="86.0" prefWidth="52.0" text="Button" />
  8. <TextArea fx:id="text" layoutX="14.0" layoutY="39.0" prefHeight="44.0" prefWidth="127.0" />
  9. </children>
  10. </Pane>
Head.xml:
Листинг программы
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.SplitPane?>
  3. <?import javafx.scene.control.TabPane?>
  4. <?import javafx.scene.layout.AnchorPane?>
  5. <?import javafx.scene.layout.ColumnConstraints?>
  6. <?import javafx.scene.layout.GridPane?>
  7. <?import javafx.scene.layout.RowConstraints?>
  8. <SplitPane dividerPositions="0.1649048625792812" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="475.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
  9. <items>
  10. <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
  11. <children>
  12. <GridPane fx:id="GridPanelBorde" prefHeight="75.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
  13. <columnConstraints>
  14. <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  15. </columnConstraints>
  16. <rowConstraints>
  17. <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  18. </rowConstraints>
  19. </GridPane>
  20. </children></AnchorPane>
  21. <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
  22. <children>
  23. <TabPane fx:id="TabPanel1" layoutY="-1.0" prefHeight="372.0" prefWidth="598.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
  24. </children></AnchorPane>
  25. </items>
  26. </SplitPane>
Main.java:
Листинг программы
  1. package application;
  2. import javafx.application.Application;
  3. import javafx.fxml.FXML;
  4. import javafx.fxml.FXMLLoader;
  5. import javafx.stage.Stage;
  6. import javafx.scene.Parent;
  7. import javafx.scene.control.TabPane;
  8. import javafx.scene.layout.GridPane;
  9. import javafx.scene.Scene;
  10.  
  11. public class Main extends Application {
  12. @FXML
  13. TabPane TabPanel1;
  14. @FXML
  15. GridPane GridPanelBorde;
  16. @Override
  17. public void start(Stage primaryStage) {
  18. try {
  19. Parent root = FXMLLoader.load(getClass().getResource("Head.fxml"));
  20. Scene scene = new Scene(root);
  21. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  22. primaryStage.setScene(scene);
  23. primaryStage.setTitle("Главная");
  24. primaryStage.show();
  25. } catch(Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. public static void main(String[] args) {
  30. launch(args);
  31. }
  32. }

Решение задачи: «Как подгрузить fxml»

textual
Листинг программы
  1. import javafx.application.Application;
  2. import javafx.fxml.FXMLLoader;
  3. import javafx.scene.Parent;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.SplitPane;
  6. import javafx.scene.layout.GridPane;
  7. import javafx.stage.Stage;
  8.  
  9.  
  10. public class Main extends Application {
  11.  
  12.     public static void main(String[] args) {
  13.         launch(args);
  14.     }
  15.  
  16.     @Override
  17.     public void start(Stage primaryStage) {
  18.         try {
  19.             Parent root = FXMLLoader.load(getClass().getResource("head.fxml"));
  20.  
  21.             SplitPane splitPane = (SplitPane) root.lookup("#split_pane");
  22.             GridPane pane = ((GridPane) splitPane.getItems().get(0).lookup("#grid_pane"));
  23.  
  24.             Parent simple = FXMLLoader.load(getClass().getResource("simple.fxml"));
  25.             pane.getChildren().add(simple);
  26.  
  27.             Scene scene = new Scene(root);
  28. //            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  29.             primaryStage.setScene(scene);
  30.             primaryStage.setTitle("Главная");
  31.             primaryStage.show();
  32.         } catch (Exception e) {
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.  
  37. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 3.846 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут