How to interact between windows in javaFX.



  • There's a basic window, a field and a button. The button is pressed with a fancy window with a text field and a button. We're putting the text in the text field, press the button and the main window field should change. How do you do that? I don't want to do a bunch of static fields, maybe there's some way better? (both windows - different classes)



  • import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Dialog;
    import javafx.scene.control.TextInputDialog;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    import java.util.Optional;
    
    public class TestSO extends Application
    {
        public static void main(String[] args)
        {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) throws Exception
        {
            BorderPane pane = new BorderPane();
            Button btn = new Button("Text");
            btn.setOnAction(e -> {
                Dialog<String> dialog = new TextInputDialog("Enter new text here");
                dialog.setTitle("Change button text");
                Optional<String> optional = dialog.showAndWait();
                optional.ifPresent(btn::setText);
            });
            pane.setCenter(btn);
            Scene scene = new Scene(pane, 500, 500);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    }
    

    In the example given, when applied to the button, there is a modular window with a text field and a button. When the text is introduced into the field and then pressed on the button, the text is changed on the button.

    P. S. 8 java.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2