简体   繁体   English

在JavaFX的GridPane中从TextArea获取文本

[英]Getting text from TextArea in a GridPane in JavaFX

I have a project in which I ask a user what type of electronic devices they use to tell them how many solar panels and batteries they need. 我有一个项目,询问用户使用什么类型的电子设备来告诉他们需要多少个太阳能电池板和电池。

To collect information, I use a gridPane with three columns, in which there are TextArea, and a variable amount of rows. 为了收集信息,我使用了具有三列的gridPane,其中有TextArea和可变数量的行。 I need to collect the information in the second and third row of the gridPane. 我需要在gridPane的第二行和第三行中收集信息。

I use the following method which I found on another question asked here. 我使用以下方法,该方法是我在这里问到的另一个问题所发现的。

private void calculer(ActionEvent event)
{
    double ampApp = 0;
    double heureApp = 0;
    double wattTotal = 0;
    double nbJourAuto;
    double heureSoleil;
    int nbPanneau;
    int nbBatterie;
    String stringTemp;

    for(int i =1; i < rangee; i++)
    {
        stringTemp = getNodeByRowColumnIndex( i, 1, gridApp).toString();
        ampApp += parseDouble(stringTemp);
    }

    for(int i =1; i < rangee; i++)
    {
        stringTemp = getNodeByRowColumnIndex(i, 2, gridApp).toString();
        heureApp += parseDouble(stringTemp);
    }

(it's only the first part of the method, I know there is a missing "}") (这只是方法的第一部分,我知道这里缺少“}”)

And here is the getNodeByRowColumnIndex code: 这是getNodeByRowColumnIndex代码:

public Node getNodeByRowColumnIndex( final int row, final int column, GridPane gridPane) {
   Node result = null;
   ObservableList<Node> childrens = gridPane.getChildren();
   for(Node node : childrens) 
   {
        if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) 
        {
           result = node;
           break;
        }
   }
   return result;
}

I tried this method, but keep getting java.lang.NullPointerException 我尝试了此方法,但一直在获取java.lang.NullPointerException

Does anyone knows why and how to solve this issues? 有谁知道为什么以及如何解决这个问题?

Edit : 编辑:

As requested here's the stack trace 根据要求,这里是堆栈跟踪

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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
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: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)
... 48 more
Caused by: java.lang.NullPointerException
at systemesolaire.FXMLDocumentController.getNodeByRowColumnIndex(FXMLDocumentController.java:111)
at systemesolaire.FXMLDocumentController.calculer(FXMLDocumentController.java:81)
... 58 more

and here's how I intialize my children (I used SceneBuilder so here is the FXML file) 这是我初始化孩子的方式(我使用了SceneBuilder,所以这里是FXML文件)

<content>
        <GridPane fx:id="gridApp" prefHeight="141.0" prefWidth="452.0">
           <columnConstraints>
              <ColumnConstraints hgrow="SOMETIMES" maxWidth="226.0" minWidth="10.0" prefWidth="226.0" />
              <ColumnConstraints hgrow="SOMETIMES" maxWidth="146.0" minWidth="10.0" prefWidth="123.0" />
              <ColumnConstraints hgrow="SOMETIMES" maxWidth="113.0" minWidth="10.0" prefWidth="103.0" />
           </columnConstraints>
           <rowConstraints>
              <RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
              <RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
              <RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
           </rowConstraints>
           <children>
              <Label text="  Nom de l'appareil">
                 <font>
                    <Font name="Anke" size="18.0" />
                 </font>
              </Label>
              <Label text="Ampérage" GridPane.columnIndex="1">
                 <font>
                    <Font name="Anke" size="14.0" />
                 </font>
              </Label>
              <Label text="Heures utilisation" GridPane.columnIndex="2">
                 <font>
                    <Font name="Anke" size="13.0" />
                 </font>
              </Label>
              <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Nom" GridPane.rowIndex="1" />
              <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Amp" GridPane.columnIndex="1" GridPane.rowIndex="1" />
              <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Heure" GridPane.columnIndex="2" GridPane.rowIndex="1" />
              <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Nom" GridPane.rowIndex="2" />
              <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Amp" GridPane.columnIndex="1" GridPane.rowIndex="2" />
              <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Heure" GridPane.columnIndex="2" GridPane.rowIndex="2" />
           </children>
        </GridPane>
     </content>

And here is How I add a row to my gridPane 这是我如何在gridPane中添加一行

 @FXML
private void ajouteRangee(ActionEvent event) 
{

   TextArea nom =  new TextArea();
   TextArea amp = new TextArea();
   TextArea heure = new TextArea();



   nom.setPromptText("Nom");
   amp.setPromptText("Amp");
   heure.setPromptText("heure");


   gridApp.addRow(rangee, nom, amp, heure);
   gridApp.layout();
   rangee++;

}

I got the answer with the help of my teacher, if anybody has the same issue, here's how I solved it. 在老师的帮助下,我得到了答案。如果有人遇到同样的问题,这就是我的解决方法。

The NullPointerException came for the getColumnIndex, the solution bypasses the issue, but I don't know why I was getting this exception in the first place. NullPointerException用于getColumnIndex,该解决方案绕过了该问题,但是我不知道为什么首先要获得此异常。

Since the amount of rows and columns is known, there is no need to search every node for the right one. 由于行和列的数量是已知的,因此无需在每个节点中搜索正确的行和列。 Also, since the number of children per row is the same, there is a logical way to get a specific children. 另外,由于每行的子代数相同,因此有一种逻辑方法可以获取特定的子代。 Here's how : 这是如何做 :

 public TextArea getNodeByRowColumnIndex( final int row, final int column, GridPane gridPane) 
{

   ObservableList<Node> childrens = gridPane.getChildren();
   TextArea result = (TextArea) childrens.get((row*3)+column);

   return result;
}

The ObservableList gets children from left to right row by row, so you can get to a specific object in the list with this formula (((rowIndex + 1) * the amount of object per row) + on which column is your object) ObservableList从左到右逐行获取子对象,因此您可以使用此公式到达列表中的特定对象((((rowIndex + 1)*每行对象的数量)+您的对象所在的列)

And then you can get the TextArea text by doing: 然后,您可以通过执行以下操作获取TextArea文本:

 stringTemp = getNodeByRowColumnIndex( i, 1, gridApp).getText();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM