Javafx 将数据插入另一个fxml文件中的网格窗格

Javafx 将数据插入另一个fxml文件中的网格窗格,javafx,javafx-8,fxml,fxmlloader,Javafx,Javafx 8,Fxml,Fxmlloader,我的第一个fxml(按钮): 我的主要意见是: package fxml_grid_test; import java.io.IOException; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import j

我的第一个fxml(按钮):

我的主要意见是:

package fxml_grid_test;

import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Fxml_grid_test extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("button_fxml.fxml"));

        Scene scene = new Scene(root, 300, 275);

        primaryStage.setTitle("insert data test");
        primaryStage.setScene(scene);
        primaryStage.show();

    }


    public static void main(String[] args) {
        launch(args);
    }

}
这个想法是: 当我按下按钮时,我想在gridpane中插入一些数据,然后显示fxml。
我的两个fxml共享控制器,我觉得这是问题的根源,但我不知道如何解决它。

控制器是编译类,实现fxml定义的对象层次结构的“代码隐藏”。这些方法对于定义操作UI元素的方法非常有用,如果需要,其他类也可以调用这些方法

在您的情况下,我们需要为
grid.fxml
定义一个控制器,因为我们要创建一个方法,该方法将负责向网格添加元素。此方法将在
MainController
中调用,该方法负责加载
grid.fxml
并更新它

帮助我们获取fxml中引用的控制器实例

因此,您可以为每个fxml文件使用两个不同的控制器。对于带有按钮的fxml,让我们将控制器称为
ParentController
,因为它负责加载第二个fxml

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class ParentController implements Initializable {

    @FXML
    private Button insertButton;

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

    }

    @FXML
    private void insertData() throws IOException {
        Stage newStage = new Stage();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("gridpane.fxml"));
        Parent root = loader.load();
        //Get controller instance from the FXMLLoader
        GridPaneController controller = loader.getController();
        controller.updateGridPane();
        Scene scene = new Scene(root, 500, 350);
        newStage.setScene(scene);
        newStage.show();
    }
}
如您所见,我们正在获取控制器实例,然后尝试调用GridPaneController类中的方法

GridPaneController controller = loader.getController();
controller.updateGridPane();
updateGridPane()
包含更新
GridPane
的逻辑,如果需要在GridPane上设置某些内容,可以将参数包含到方法中并使用它们

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;

import java.net.URL;
import java.util.ResourceBundle;

public class GridPaneController implements Initializable {

    @FXML private GridPane gpane;

    public void updateGridPane() {
        Label newLabel = new Label("test");
        gpane.add(newLabel, 0, 0);
    }

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

    }
} 
FXML没有什么特别的功能,它只是有不同的控制器

grid.fxml

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.GridPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.GridPaneController">
  <top>
    <GridPane fx:id="gpane">
    </GridPane>
  </top>
</BorderPane>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.ParentController" >
    <Button fx:id="insertButton" text="insert" onMouseClicked="#insertData"/>
</StackPane>

main.fxml

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.GridPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.GridPaneController">
  <top>
    <GridPane fx:id="gpane">
    </GridPane>
  </top>
</BorderPane>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.ParentController" >
    <Button fx:id="insertButton" text="insert" onMouseClicked="#insertData"/>
</StackPane>


单击按钮即可将动态数据添加到网格窗格中。但是,我不知道你想在这里做什么。你能创建一个并将其添加到你的问题中吗?简单地说,我在fxml_1中有两个fxml(fxml_1和fxml_2),我有一个按钮,在fxml_2中我有一个网格窗格。当我按下按钮时,我想在fxml_2中的网格窗格中插入一些数据,然后显示fxml。是的,这是可以实现的。虽然如果你能提供一个简单的例子,解释起来会更容易。顾名思义,“sqlexception”不是JabaFX相关的错误。添加该异常的stacktrace并相应地更改问题的标记。顺便问一下,你所说的“…所以我决定手动尝试”是什么意思?你手动做了什么?我添加了更简单的代码。希望你们能帮助我。谢谢,先生,它工作得很好,如果你不介意的话,你能简单地解释一下initializable的用途吗?覆盖
initialize()
。当相关文档的内容已完全加载时,此方法在实现控制器上调用一次,并用于对内容执行任何必要的后处理。
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.ParentController" >
    <Button fx:id="insertButton" text="insert" onMouseClicked="#insertData"/>
</StackPane>