faraaz
faraaz

Reputation: 128

How to execute code when a new window opens in JavaFX?

I want the code in execute() to be executed when the window the controller belongs to is opened. How would I do so?

public class OpenAccountScreenController {
    public TableView openAccountTableView = new TableView();
    public TableColumn accountNameColumn = new TableColumn();
    public TableColumn accountNumberColumn = new TableColumn();

    public void execute() {
        accountNameColumn.setCellValueFactory(new PropertyValueFactory("accountName"));
        accountNumberColumn.setCellValueFactory(new PropertyValueFactory("accountNumber"));

        for(Account account : accounts.values()) {
            openAccountTableView.getItems().add(account);
        }
    }
}

Upvotes: 0

Views: 1024

Answers (1)

Yahya
Yahya

Reputation: 14072

If I understand your question properly that you want to call execute() in another class once the Window/Stage is shown, so you can do something like this:

OpenAccountScreenController obj = new OpenAccountScreenController();

stage.setOnShowing(e-> obj.execute());

Otherwise please clarify.

Upvotes: 1

Related Questions