【问题标题】:Copy entite fxml data to different container将实体 fxml 数据复制到不同的容器
【发布时间】:2015-10-03 04:39:47
【问题描述】:

我有两个 vbox。

第一个 vbox fx:id 是 vbox1

第二个 vbox fx:id 是 vbox2

在 vbox1 中,我有文本框、组合框、按钮和其他所有内容。

我有一个按钮想要复制(onclick)整个源/fxml 从 vbox1 到 vbox2。

有没有办法做到这一点?

【问题讨论】:

    标签: javafx copy javafx-8 vbox


    【解决方案1】:

    在单独的 FXML 文件中定义 VBoxes 的内容。您可以将第一个VBox 中的内容直接包含在您的“主” fxml 中,并带有<fx:include>:

    <VBox fx:id="vbox1">
        <fx:include source="Content.fxml"/>
    </VBox>
    

    然后你可以在按钮的处理程序中加载另一个副本

    @FXML
    public void handleButtonAction(ActionEvent e) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Content.fxml"));
        vbox2.getChildren().add(loader.load());
    }
    

    完整示例(在名为 application 的包中的所有内容):

    Main.fxml:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.layout.BorderPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.control.Button?>
    
    <BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
        <center>
            <HBox spacing="5">
                <VBox fx:id="vbox1">
                    <fx:include source="Content.fxml"/>
                </VBox>
                <VBox fx:id="vbox2"/>
            </HBox>
        </center>
        <bottom>
            <Button text="Load" onAction="#load" BorderPane.alignment="CENTER"/>
        </bottom>
    </BorderPane>
    

    MainController.java:

    package application;
    
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.layout.VBox;
    
    public class MainController {
        @FXML
        private VBox vbox1 ;
        @FXML
        private VBox vbox2 ;
        @FXML
        private void load() throws Exception {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("Content.fxml"));
            vbox2.getChildren().add(loader.load());
        }
    }
    

    内容.fxml:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.control.ComboBox?>
    <?import javafx.collections.FXCollections?>
    
    <?import java.lang.String?>
    <?import javafx.scene.control.Button?>
    
    <VBox xmlns:fx="http://javafx.com/fxml/1">
        <TextField promptText="Text Field"/>
        <ComboBox>
            <items>
                <FXCollections fx:factory="observableArrayList">
                    <String fx:value="One"/>
                    <String fx:value="Two"/>
                    <String fx:value="Three"/>
                </FXCollections>
            </items>
        </ComboBox>
        <Button text="Click me"/>
    </VBox>
    

    Main.java:

    package application;
    
    import javafx.application.Application;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;
    import javafx.fxml.FXMLLoader;
    
    
    public class Main extends Application {
        @Override
        public void start(Stage primaryStage) {
            try {
                BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
                Scene scene = new Scene(root,400,400);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2016-02-24
      • 2018-12-20
      • 2019-02-11
      相关资源
      最近更新 更多