-1

I want open main window, and, after it, in moment, when it opened, open dialouge window (in which I choice some parameters), whithout clicking or typing anything. Dialouge window must open as such. Where I should write code, which open dialouge window?

  • reading the api doc seems to have come into disuse ;) It's quite easy: go to the online fx java doc, type "Window" into the search field, start reading the methods/properties that are available and then write a small code example that tries to use what you found ... – kleopatra Dec 16 '18 at 11:58

1 Answers1

1

You can use the Window.onShown property. The EventHandler is invoked for WINDOW_SHOWN events which, as one would expect, are fired once the Window has been shown. Here's a small example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.Window;

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setOnShown(event -> showDialog(primaryStage));
    primaryStage.setScene(new Scene(new StackPane(new Label("Hello, World!")), 600, 400));
    primaryStage.setTitle("JavaFX Application");
    primaryStage.show();
  }

  private void showDialog(Window owner) {
    Alert alert = new Alert(AlertType.INFORMATION);
    alert.initOwner(owner);
    alert.setContentText("This is a dialog shown immediately after the window was shown.");
    alert.show();
  }

}
Slaw
  • 37,820
  • 8
  • 53
  • 80
  • Thank you very much, and, may be you can advice me, can I write method showDialog(Window owner) in initialize() method of MainController? – Pavel_Kisliuk Dec 17 '18 at 09:45
  • You _can_ but you probably don't want to. The `initialize` method is invoked during the call to one of the `load` methods of `FXMLLoader`. This means the generated `Node` tree has not been added to a `Scene` or, by extension, a `Window`. And, in typical implementations, the method is invoked _before the window is shown_. However, you can expose a method on the controller which you'd call from the `Application` class. You'd do this by [getting the controller](https://stackoverflow.com/questions/10751271/accessing-fxml-controller-class) and calling the method inside the `onShown` handler. – Slaw Dec 17 '18 at 10:05