Tom Kuiper
Tom Kuiper

Reputation: 1

How do you do one action through multiple event handlers in Java FX?

I am trying to make a form that does the quadratic formula. It works on command prompt, but I want it to work on a form, and for it to update the output every time you change the input. This is my form so far. There are multiple areas you can change, which are the combo box and the text fields, but I want all of them to update the labels to show an output. This is where my problem is. I do not know how to have something similar to a method that updates the labels. A simplified version of the code is below:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class SmallForm extends Application{

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane grid = new GridPane();
        grid.setVgap(5);

        Label lblConcatenated = new Label("");//label that will hold the concatenated string
        grid.add(lblConcatenated, 0, 0);

        TextField txtA = new TextField("");//Text field one
        grid.add(txtA, 0, 1);

        TextField txtB = new TextField("");//Text field two
        grid.add(txtB, 0, 2);

        txtA.textProperty().addListener(new ChangeListener<String>() { //Triggers whenever the second text field is changed
            @Override public void changed(ObservableValue ov, String t, String t1) {
               lblConcatenated.setText(txtA.getText() + txtB.getText());//Concatenates the text in the two text fields
            }
        });

        txtB.textProperty().addListener(new ChangeListener<String>() {
            @Override public void changed(ObservableValue ov, String t, String t1) { //Triggers whenever the second text field is changed
                lblConcatenated.setText(txtA.getText() + txtB.getText());//Concatenates the text in the two text fields
            }
        });

        Scene scene = new Scene(grid, 250, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

My goal is to make the concatenation line only have to be written once. This would be a lot more prevalent with more code that was repeated. In command prompt this would be easily done with a method, but that doesn't work in java fx to my knowledge. I've heard the term threading used to do this, but I couldn't figure it out.

Thank you!

Upvotes: 0

Views: 76

Answers (1)

fabian
fabian

Reputation: 82451

You could of course use a methods. You could also use the same ChangeListener<String> object for both properties.

In this case however I recommend using a binding:

lblConcatenated.textProperty().bind(txtA.textProperty().concat(txtB.textProperty()));

If you need to parse the values and do updates based on the parsed values, I recommend using a TextFormatter:

StringConverter<Double> converter = new DoubleStringConverter();
TextFormatter<Double> formatterA = new TextFormatter<>(converter, 0d);
TextFormatter<Double> formatterB = new TextFormatter<>(converter, 0d);
txtA.setTextFormatter(formatterA);
txtB.setTextFormatter(formatterB);

formatterA.valueProperty()...

The formatter values are updated when the TextField looses focus or you trigger the onAction event of the TextField.

Upvotes: 1

Related Questions