0

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.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • Hey, thanks for the reply. I already linked my Controller with my Fxml. I also can call Methods, but not the one with parameters. Am I missing something in that question provided by you ? –  Jun 15 '19 at 01:10
  • I added an answer. The point is you can't link arbitrary methods to nodes, but the event handlers methods can themselves call the other method(s) you want to invoke. – Slaw Jun 15 '19 at 01:14
  • Why do you implement parsing the input like this? There's always exactly one iteration of the loop so why use one? Furthermore assuming the input is valid you always overwrite the value passed as `start` parameter, so why bother passing it; same for `length`. Also the compiler should complain about you adding a statement after exiting a `catch` block using a `throw` statement. Why don't you just parse the input in a event handler and call the original `fibonacci` method with the results as parameter (you may need to do some work to get the output right). – fabian Jun 15 '19 at 01:39

1 Answers1

1

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.

Slaw
  • 37,820
  • 8
  • 53
  • 80