如何从第二个控制器类中获取主控制器类中textArea中的选定文本?

时间:2019-08-07 01:45:21

标签: java javafx fxml

我正在尝试从FXMLDocumentController.java中的textArea获取所选文本,并将其传递给第二个控制器类HeadingsController.java。

我正在Netbeans中运行该应用程序,它启动并加载正常。当我单击带有fx:id =“ h1Link”的超链接时,出现nullPointerException。空的pointerException在HeadingsController.java的第27行表示其为

String selectedText = fxmlDocC.getSelectedTextfromTextArea();

FXMLDocumentController.java是要尝试引用的textArea ...

package textareatest1;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;


public class FXMLDocumentController {

    @FXML public TextArea textArea;
    String selectedText = new String();
    @FXML private HeadingsController headingsController = new HeadingsController();

    public String getSelectedTextfromTextArea(){
        selectedText =  textArea.getSelectedText();
        return selectedText;
    }

    public void replaceSelectedText(String string){
        textArea.replaceSelection(string);
    }

    @FXML public void initialize() {
        headingsController.init(this);
        textArea.setWrapText(true);

    }   

}

这是上述控制器的FXML文件...

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:id="borderPaneRoot" prefHeight="600.0" prefWidth="1000.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="textareatest1.FXMLDocumentController" >
    <top>

    </top>
    <center>
        <TextArea fx:id="textArea" />
    </center>
    <left>
        <GridPane id="gridpaneLeft" fx:id="gridpaneLeft" hgap="10.0" prefHeight="200.0" prefWidth="320.0" vgap="10.0" >
            <Accordion fx:id="leftAccordion" prefHeight="250.0" prefWidth="680.0">
                <panes>

                    <TitledPane fx:id="headingsTitlePane" animated="true" text="Headings">
                        <content>

                                <Pane>
                                    <children>
                                        <fx:include fx:id="fxmlheadings" source="FXMLHeadings.fxml"/>
                                    </children>
                                </Pane>
                        </content>
                    </TitledPane>

                </panes>
            </Accordion>
        </GridPane>
    </left>

</BorderPane>

这是我的第二个控制器HeadingsControllerr.java .....

package textareatest1;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;


public class HeadingsController {
    @FXML private FXMLDocumentController fxmlDocC;


    String[] headings = {"h1. ","h2. ","h3. ","h4. ","h5. ","h6. "};
    @FXML public Hyperlink h1Link;

    @FXML
    private void handleH1LinkAction(ActionEvent event) {

        String selectedText = fxmlDocC.getSelectedTextfromTextArea();
        System.out.println(selectedText);
//        System.out.println("test");
    }

    public void init(FXMLDocumentController fxmlDocumentController) {
           fxmlDocC = fxmlDocumentController;
    }
}

这是HeadingsController.java的FXML文件...

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="textareatest1.HeadingsController" fx:id="fxmlheadings">

    <Hyperlink id="links" fx:id="h1Link" text="h1. Biggest heading" layoutX="5.0" layoutY="3.0" onAction="#handleH1LinkAction"  />


</AnchorPane>

这是我得到的带有nullPointerException的堆栈跟踪,我相信这是因为fxmlDocCnull ...我想我需要正确地创建FXMLDocumentController的实例吗?

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.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:8413)
    at javafx.scene.control.Hyperlink.fire(Hyperlink.java:153)
    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.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:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    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$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
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:498)
    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:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 56 more
Caused by: java.lang.NullPointerException
    at textareatest1.HeadingsController.handleH1LinkAction(HeadingsController.java:27)
    ... 66 more

我试图通过UI突出显示TextArea中的文本,并在单击带有fx:id =“ h1Link”的超链接时在控制台中打印文本。

1 个答案:

答案 0 :(得分:0)

无法通过创建FXMLDocumentController的实例来解决此问题,而是通过确保将实例传递给的控制器是与包含的fxml一起使用的控制器来解决;您用new HeadingsController()创建的那个实例不是该实例。

如果您将FXMLLoader附加到Controller的{​​{1}}上,则fx:id会将包含元素的控制器注入到您获得的名称的字段中元素:

<fx:include>