Jump to content

JavaFX (IntelliJ) - How to retrieve String from TextField inside a TitledPane

DRAGONPASS

Hey Guys,

 

I'm currently working on a little Java programm containing a JavaFX GUI.

Since the last time i coded Java (and JavaFX) a couple of years passed and I'm a little rusty.

 

The issue I'm facing is that I'm too stupid to get a String Value from a Textfield inside a TitledPane.

I remember doing this was quite simple and I did this a lot a couple of years ago (Quite embarrasing that I remember how to use Tasks and Threads but not how to code this stupid little thingy).

 

So what I wanna do is a Settings section inside a a TitledPane (see screenshot).

The relevant code inside my FXML fikle looks like this:

[...]

<TitledPane id="settingsPane" fx:id="settings" animated="false" expanded="false" layoutX="14.0" layoutY="130.0" onMouseClicked="#openSettings" prefHeight="140.0" prefWidth="257.0" text="Settings">
  <content>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="75.0" prefWidth="255.0">
         <children>
            <Label layoutX="14.0" layoutY="14.0" text="Price per period (in €):" />
            <Label layoutX="14.0" layoutY="46.0" text="Period length (in min.):"/>
            <TextField id="setPrice" layoutX="144.0" layoutY="10.0" prefHeight="25.0" prefWidth="100.0"  promptText="Default" />
            <TextField id="setMin" layoutX="144.0" layoutY="42.0" prefHeight="25.0" prefWidth="100.0"  promptText="Default" />
            <Button layoutX="197.0" fx:id="apply" layoutY="75.0" onMouseClicked="#applySettings" mnemonicParsing="false" text="Apply" />
         </children></AnchorPane>
  </content>
</TitledPane>

[...]

 

"setPrice" is the upper textfield, "setMin" is the lower one. The button "apply" has an "onMouseClicked" Action which is placed in a seperate Controller class.

What I want to achieve is that the user types in his settings into the TextFields and after hitting apply they shall be written to certain variables.

 

Relevant code inside the controller class looks like this (I didn't code the "write to variable" part yet but just wanted to print the TextField Values to System.out):

[...]

public TitledPane settings;
public TextField setPrice;
public TextField setMin;
public Button apply;

[...]

public void applySettings(MouseEvent mouseEvent) {
    System.out.println(setPrice.getText());
    System.out.println(setMin.getText());
}

[...]

 

So now to the issue:

When I write something into both TextFields and hit the apply button I'm getting an NPE - I assume because the Input wasn't actually applied to the TextFields.

How do I solve this situation.

 

Thanks in advance for every answer! 🙂 Please be gentle - I've become quite a noob regarding Java.

 

 

 

titledpane_scenebuilder.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

public void applySettings(MouseEvent mouseEvent) {
    System.out.println(setPrice.getText());
    System.out.println(setMin.getText());
}

If this method results in an NPE, then most likely setPrice and/or setMin are null. I'd set a break point and run with a debugger. Alternatively add a null check and see if the error still happens.

 

Where do you instantiate these fields? How are those fields (or member variables) bound to the controls visible on the form?

 

public TitledPane settings;

This simply declares a variable. Unless you instantiate it somewhere or assign a value to it, it will be initialized to its default value (null). So you need something like

settings = new TitledPane();
# —or—
settings = <some JavaFX method to get instance of control from form>

somewhere to be able to call its methods without running into an NPE.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

OMG I'm Stupid!! 😄

 

Looked into it after your reply and found it:

The Textfields where defined in my controller class but I somehow deleted the fx:id's of them  in the coding process.

So I was trying to <textfield>.getText() from a Textdield that didn't exist -> NPE

After adding the fx:id's again to the FXML file i got the values of the two fields print to System.out

 

<TextField id="setPrice" fx:id="setPrice" layoutX="144.0" layoutY="10.0" prefHeight="25.0" prefWidth="100.0"  promptText="Default" />
<TextField id="setMin" fx:id="setMin" layoutX="144.0" layoutY="42.0" prefHeight="25.0" prefWidth="100.0"  promptText="Default" />

 

(the parameters that are red where missing)

 

Thanks for your help though ! 🙂 Now I can start writing the Userinput into the destination variables

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×