K
K
kb_s_fs2019-04-17 23:00:26
Java
kb_s_fs, 2019-04-17 23:00:26

How to access variables from Controller(javafx) from other classes?

First grade:

package sample;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import sample.Task1.Main;

public class Controller {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Tab mainTab = new Tab();

    @FXML
    private Tab task1 = new Tab();

    @FXML
    private TextField lineOfNumbers = new TextField();

    @FXML
    private Button addInfo1 = new Button();

    @FXML
    public static TextArea sortedNumbers = new TextArea();

    @FXML
    private Tab task2;

    @FXML
    private TextField numberOfObjects;

    @FXML
    private Button addInfo2;

    @FXML
    private TextArea resultOfMethods;

    @FXML
    private Tab task3;

    @FXML
    private TextField numberOfParcels;

    @FXML
    private TextArea resultOfParcels;

    @FXML
    private Button addInfo3;

    @FXML
    private Tab task4;

    @FXML
    private TextField numberOfWorkers;

    @FXML
    private TextField numberOfDetails;

    @FXML
    private TextArea resultOfWork;

    @FXML
    private Button addInfo4;

    @FXML
    private Button addInfo5;

    private static String[] args;
    @FXML
    void initialize() {
        addInfo1.setOnAction(e -> {
            sortedNumbers.setText("");
            args = new String[1];
            args[0] = lineOfNumbers.getText();
            Main.main(args);
        });

    }
}

Second class:
package sample.Task1;
import sample.Controller;

public class Main {
    static public void main(String[] args) {

        if (args[0].length() == 0) {
            Controller.sortedNumbers.appendText("You should write one parameter!");
            return;
        }

        try {
            char[] symbols = args[0].toCharArray();
            for (int i = 0; i < symbols.length; i++) {
                if (symbols[i] == ' ') {
                    Controller.sortedNumbers.appendText("You should write one parameter!");
                    return;
                }
            }
            String[] num_array = new String[symbols.length];
            boolean flag = false;
            boolean minus = false;
            int num = 0;
            int index = 0;

            for (int i = 0; i < symbols.length; i++)
                num_array[i] = "";

            for (int i = 0; i < (symbols.length - 1); i++) {
                if (((symbols[i + 1] >= '0') && (symbols[i + 1] <= '9')) && (symbols[i] == '-')) {
                    minus = true;
                }

                if (!((symbols[i + 1] >= '0') && (symbols[i + 1] <= '9')) && (symbols[i] == '-')) {
                    minus = false;
                }

                if (!((symbols[i] >= '0') && (symbols[i] <= '9')) && !(symbols[i] == '-')) {
                    minus = false;
                }

                if ((symbols[i] >= '0') && (symbols[i] <= '9') && !(minus)) {
                    flag = true;
                    num_array[index] += symbols[i];
                    if ((i == (symbols.length - 2)) && ((symbols[i + 1] >= '0') && (symbols[i + 1] <= '9'))) {
                        num = 1;
                    }
                } else {
                    if (flag) {
                        index++;
                        flag = false;
                    } else {
                        continue;
                    }
                }
            }

            index++;

            if (num == 1) {
                num_array[index - 1] += symbols[symbols.length - 1];
            } else {
                if ((symbols[symbols.length - 1] >= '0') && (symbols[symbols.length - 1] <= '9') && (minus == false)) {
                    num_array[index - 1] = "0" + symbols[symbols.length - 1];
                }
            }

            if ((index == 1) && (num_array[index - 1] == "")) {
                Controller.sortedNumbers.appendText("There aren't non-positive numbers!\n");
                return;
            }

            if ((index == 2) && (num_array[index - 1] == "")) {
                index--;
            }

            if (num_array[index - 1] == "") {
                index--;
            }
            int numbers[] = new int[index];

            for (int i = 0; i < index; i++) {
                numbers[i] = Integer.parseInt(num_array[i]);
            }

            int temp;
            for (int i = 0; i < index; i++) {
                for (int j = 0; j < index; j++)
                    if (numbers[i] < numbers[j]) {
                        temp = numbers[i];
                        numbers[i] = numbers[j];
                        numbers[j] = temp;
                    }
            }

            //Controller.sortedNumbers.appendText("String as the paramater - " + args[0] + "\n");

            Controller.sortedNumbers.appendText("Numbers founded in this string: \n");
            for (int i = 0; i < index; i++) {
                System.out.println(((i + 1) + ": " + num_array[i]) + "\n");
            }


            Controller.sortedNumbers.appendText("Sorted numbers: \n");
            for (int i = 0; i < index; i++) {
                Controller.sortedNumbers.appendText(((i + 1) + ": " + numbers[i]) + "\n");
            }

        }
        catch(NumberFormatException err) {
            Controller.sortedNumbers.appendText("NumberFormatException: " + err.getMessage());
        }
    }
}

If I output the data in sortedNumbers nothing happens. If I output in the console, then everything works. How can I make it output in a TextArea (i.e. sortedNumbers)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Scorpio, 2019-04-22
@notAquarius

The idea is simple, you should get an instance of the Controller class

FXMLLoader fxmlLoader = new FXMLLoader(бла бла бла бла);
Controller controller = (Controller) fxmlLoader.getController();

Now you have an object of class Controller
Next, in the controller, create a method that is responsible for changing your TextArea;
(method name can be anything)
public void setTextInTextArea(String string){
//Далее ты можешь вызывать этот метод, но т.к. он вызывается из другого потока , в твоем //
//случае главного , ты должен написать так:
Platform.runLater(()->{
//Здесь уже изменяй свой TextArea
});}

You are already changing the text in the TextArea from your Main method
@FXML
private TextField lineOfNumbers = new TextField();
@FXML
private Button addInfo1 = new Button();
@FXML
public static TextArea sortedNumbers = new TextArea();

@FXML
private TextField lineOfNumbers ;
@FXML
private Button addInfo1 ;
@FXML
public TextArea sortedNumbers;

UPD: I could make syntax errors in the code, so Sorry, I don't have a PC at hand, I'm on the road. Well, do not rewrite the code thoughtlessly, but try to get to the bottom of it. Read about multithreading in JavaFx, how FXMLLoader works, about @FXML variables in the controller. Good luck.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question