I
I
igor2020-06-07 17:09:00
Java
igor, 2020-06-07 17:09:00

Why can't you instantiate a class in the initialize() of another class?

Java FX. ran into a problem. In order for something to change in another scene when the button of one scene is pressed, you need to go into initialize () and create an instance of the class in order to use its components, which is why the program gives an error when you click on that very button and does nothing.

public void initialize()
    {
        SceneSecondary SS = new SceneSecondary();         //экземпляр другого класса, в котором кнопка
        SS.nextPageButton1.setOnAction(e -> {             //использую эту кнопку из другого класса
            characterList.getItems().add("test_item");    //не выходит
        });
    }


What to do in this case? This is the only solution and it doesn't work for me!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2020-06-07
@jamakasi666

initialize is not intended for this purpose. To get started, read how JavaFX works, be surprised, but take away the new word MVC.
When understanding comes, devices will understand that at least each form needs to be divided into at least 2 parts, specifically into model and (combined view and controller). In this case, your Model will store the data, their state and operate on them. The combined View \ Controller will contain the form itself (View) and the controller (Controller) layer connecting the model with the view. and binding events (the button was pressed or the data changed) in both directions.
Specifically, the code you provided is nonsense and will not work. You already have a current scene and assume SceneSecondary , and you create another SceneSecondary that is not related to the first one in any way. they are completely different scenes.
Well, before writing with indignation that this is the only solution (again, from somewhere you personally contrived), you can answer yourself what to do, read the basics and the questions will disappear by themselves and spend much less time than googling the incomprehensible and writing an incomprehensible and stupid question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question