user11650392
user11650392

Reputation:

Calling a method with parameters in javafx´s scenebuilder

I started coding two weeks ago and I´m trying java higgledy-piggledy.

I wrote a Java Fibonacci Sequence program, which let´s you select the start of your sequence and the length.

I build my first JavaFx Gui with code, but then changed to SceneBuilder.

Since then I´m trying to get my code implemented in the sample.fxml and Controller.java. I figured out how to call methods and link them with my gui with scene builder, but I can´t quite figure out how to call a method with parameters, like my fibonacci method.

Sorry, this is my first question. Don´t hate me for making mistakes. Thanks in advice.

This worked perfectly fine with my hand coded gui, because i could just use my method on an Object and call a triggering event with the method.

Google is my friend, but I can´t find any solutions about this topic yet.

I was thinking about using a declared variable in my code instead of the parameters and then call them with a setter, but I have no idea how to implement it in Scene Builder.

i only can link methods without parameters (events work) (

void fibonacci(int start, int length) { 

            while (i <= start) {  (not everything due to understainding)
       //the syntax of my method in "original" javafx getting called by


        if (actionEvent.getSource() == sub_button) {
            FibMain fibMain = new FibMain();
            while (true) {


                try {
                    length = Integer.parseInt(fib_length.getText());    

                    start = Integer.parseInt(fib_start.getText());
                    break;
                } catch (NumberFormatException e) {
                    System.out.println("ERROR!");
                    fib_out.setText("ERROR! ONLY NUMBERS ARE ALLOWED!");
                    throw e; fibMain.fibonacci(start, length);      }

This is my working code and i would love to simple call the method with the start and length parameter.

I expect to be able to call the Method with the parameters start and length in SceneBuilder and link them to an ActionEvcent.

Upvotes: 0

Views: 4276

Answers (1)

Slaw
Slaw

Reputation: 46255

I'm assuming you have two TextFields for the different inputs and something like a Button to start the computation. The method you link to your Button should parse the text of each TextField into ints and then invoke your fibonacci method. You can get access to your TextFields by injecting them into the controller instance. This is done by giving them an fx:id attribute in the FXML file where the value matches the name of a field in the controller class.

Example FXML:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>

<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" 
      fx:controller="com.example.Controller" alignment="CENTER" spacing="10">

    <padding>
        <Insets topRightBottomLeft="15"/>
    </padding>

    <TextField fx:id="startField" promptText="Start"/>

    <TextField fx:id="lengthField" promptText = "Length"/>

    <Button text="Compute" onAction="#handleCompute"/>

</VBox>

Example controller:

package com.example;

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

public class Controller {

    // Will be injected by FXMLLoader
    @FXML private TextField startField;
    @FXML private TextField lengthField;

    @FXML
    private void handleCompute(ActionEvent event) {
        event.consume();

        // Omitted error handling. If there was an exception one option is to
        // show a javafx.scene.control.Alert to the user.
        int start = Integer.parseInt(startField.getText());
        int length = Integer.parseInt(lengthField.getText());

        // Needs to be defined. Do something with result?
        fibonacci(start, length);
    }

}

Note: I didn't include a way to show the user the result. However, one simple option is to include a Label, inject it into the controller the same way as the TextFields, and set its text property when the computation is complete.

Note: You can restrict what characters can be entered into a TextField by setting a TextFormatter on it. See this question.

Upvotes: 1

Related Questions