将2个变量从一个控制器传递到另一个控制器

时间:2016-02-22 19:50:11

标签: java javafx-2 fxml

我目前有3个java类(Main,FileSelector和FileDisplayer)以及2个FXML文件(FileSelector.fxml和FileDisplayer.fxml)

我的目标是最初显示一个带有3个按钮的窗口,2用于用户选择文件,第三个用于提交文件。然后,我希望该窗口关闭,另一个窗口打开2个标签,将2个文件的路径名显示为字符串。

Main.java:

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/FileSelector.fxml"));
            Scene scene = new Scene(root);
            stage.setTitle("Select Files");
            stage.setScene(scene);
            stage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

FileSelector.java:

public class FileSelector {

@FXML
private Button file1;

@FXML
private Button file2;

@FXML
private Button go;

@FXML
private Label file1Label;

@FXML
private Label file2Label;

@FXML
public void selectFile1(ActionEvent event) {
    final FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Select file1");
    File file = fileChooser.showOpenDialog(null);
    file1Label.setText(file.toString());
}

@FXML
public void selectFile2(ActionEvent event) {
    final FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Select file2");
    File file = fileChooser.showOpenDialog(null);
    file2Label.setText(file.toString());
}

@FXML
public void goButton(ActionEvent event) {
    if (!(file1Label.getText().equals("") || file2Label.getText().equals(""))) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/FileDisplayer.fxml"));

            Parent root = (Parent) loader.load();
            FileDisplayer fileDisplayer = loader.getController();
            fileDisplayer.setFile1(file1Label.getText());
            fileDisplayer.setFile2(file2Label.getText());
            Scene scene = new Scene(root);

            Stage stage = new Stage();

            stage.setScene(scene);

            stage.show();

            ((Node)(event.getSource())).getScene().getWindow().hide();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

FileDisplayer.java:

public class FileDisplayer {

    @FXML
    private Label file1Label;

    @FXML
    private Label file2Label;

    public void setFile1(String file1Path) {
        this.file1Label.setText(file1Path); 
    }

    public void setFile2(String file2Path) {
        this.file2Label.setText(file2Path);
    }

}

FileSelector.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="150.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FileSelector">
   <children>
      <Button fx:id="file1" layoutX="25.0" layoutY="20.0" minWidth="130.0" mnemonicParsing="false" onAction="#selectFile1" prefWidth="130.0" text="Select File 1" />
      <Button fx:id="file2" layoutX="25.0" layoutY="66.0" minWidth="130.0" mnemonicParsing="false" onAction="#selectFile2" prefWidth="130.0" text="Select File 2" />
      <Button fx:id="go" layoutX="230.0" layoutY="109.0" mnemonicParsing="false" onAction="#goButton" prefWidth="40.0" text="Go" />
      <Label fx:id="file1Label" layoutX="174.0" layoutY="20.0" prefHeight="25.0" prefWidth="310.0" />
      <Label fx:id="file2Label" layoutX="174.0" layoutY="66.0" prefHeight="25.0" prefWidth="310.0" />
   </children>
</AnchorPane>

FileDisplayer.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="file1Label" layoutX="68.0" layoutY="56.0" prefHeight="56.0" prefWidth="264.0" textAlignment="CENTER" />
      <Label fx:id="file2Label" layoutX="68.0" layoutY="150.0" prefHeight="56.0" prefWidth="264.0" textAlignment="CENTER" />
   </children>
</AnchorPane>

我已经跟随了其他问题的许多不同的例子,但我不断得到一个NullPointerException,除非我删除了对setfile_方法的调用,这只给了我一个空白的第二个窗口。

  

修改

这是完整的堆栈跟踪:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: java.lang.NullPointerException
    at application.FileSelector.goButton(FileSelector.java:58)
    ... 58 more

1 个答案:

答案 0 :(得分:1)

您没有fx:controller设置FileDisplayer.fxml的根节点。这意味着大概loader.getController(在FileSelector#goButton中)返回null。