O
O
O. J2015-12-07 09:43:31
Java
O. J, 2015-12-07 09:43:31

JavaFX How to re-call initialize from another initialize?

MainController in which initialize is called just once.

public class MainController {
    final Query PURGE = new Query();

    @FXML
    private Button idAdd;
    @FXML
    private Button idRemove;
    @FXML
    private ObservableList<ViewTable> usersData = FXCollections.observableArrayList();
    @FXML
    private TableView<ViewTable> idTable;
    @FXML
    private TableColumn<ViewTable, Integer> idNumColumn;
    @FXML
    private TableColumn<ViewTable, String> idNameValueColumn;
    @FXML
    private TableColumn<ViewTable, String> idCountColumn;

    @FXML
    public void initialize() throws Exception {
            initData();
            idNumColumn.setCellValueFactory(new PropertyValueFactory<ViewTable, Integer>("id"));
            idNameValueColumn.setCellValueFactory(new PropertyValueFactory<ViewTable, String>("nameValue"));
            idCountColumn.setCellValueFactory(new PropertyValueFactory<ViewTable, String>("count"));
            idTable.setItems(usersData);

            idAdd.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
                    try {
                        //генерируем и вызываем вторую форму, в которой соответственно вызывается уже свой initialize
                        UIGenerate uiGenerate = new UIGenerate("addForm", "AddFormStart");
                        Stage stage = uiGenerate.startUIGenerate();
                        stage.show();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
}
private void initData() {
        try{
            ResultSet resultSet = PURGE.getAll();

            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String nameValue = resultSet.getString("namevalue");
                String count = resultSet.getString("count");
                usersData.add(new ViewTable(id, nameValue, count));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//дергаем последнею запись из базы
public void lastRecord() {
        try{
            ResultSet resultSet = PURGE.getLastRecord();

            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String nameValue = resultSet.getString("namevalue");
                String count = resultSet.getString("count");
                usersData.add(new ViewTable(id, nameValue, count));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

The second form in which its own initialize is already called.
public class AddFormController extends MainController {
    @FXML
    private AnchorPane idAnchorPane;
    @FXML
    private TextField idNameValue;
    @FXML
    private TextField idCountValue;
    @FXML
    private Button idAddButton;
    @FXML
    private Button idCloseButton;
    @FXML
    private Label idMessage;

    @FXML
    public void initialize() throws Exception{

        idCloseButton.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                try {
                    UIGenerate uiGenerate = new UIGenerate("addForm", "AddFormStart");
                    Stage stage = uiGenerate.startUIGenerate();
                    stage.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        idAddButton.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {

                String nameValue = idNameValue.getText().trim();
                int countValue = Integer.parseInt(idCountValue.getText());
                Query query = new Query();

                try {
                    if (!nameValue.isEmpty()) {
                        query.queryAdd(nameValue, countValue);
                        UIGenerate uiGenerate = new UIGenerate("addForm", "AddFormStart");
                        Stage stage = uiGenerate.startUIGenerate();
                        stage.close();
                    } else {
                        idMessage.setText("Error");
                        idMessage.setVisible(true);
                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


}

Actually now, how to set up a tight connection between two different forms?
In my case, it is required that when a button located on form2 is clicked, an event on form1 is called. In this example, I'm trying to ensure that after writing data to the database on form2, after closing the form, the newly recorded data is immediately loaded into the table on form1.
Actually, the repeated call of initialize form1, from initialize form2, either did not give the proper result at all or led to java.lang.NullPointerException
Actually, I already assume that in general I have broken the logic of the entire project, and I’m clearly doing something wrong
https:// github.com/ChristianLisov/InventoryControl full code of the project, for those who are ready to point me in the right direction in order to learn zen.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Riard Brugekaaim, 2015-12-07
@OrlovEvgeny

Well, I did the interaction like this:

FXMLLoader something = new FXMLLoader(fxml);
root = something.load();

After initializing the controller, we pull it out with the following command
And pass it references to objects that need to be changed from another controller
. I had an imaginary interaction - that was enough for me.

A
Alexander Zakharov, 2019-05-15
@rootlocal

import javafx.fxml.Initializable;

public class MainController implements Initializable {

    // ...

    @FXML
    public void initialize(URL location, ResourceBundle resources) {

       // ...

    }

// ...

}

public class AddFormController extends MainController {

    // ...

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        super.initialize(location, resources);

        // ...

    }

    // ...

}

Introduction to FXML

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question