JavaFx-Communicating Between Stage

Program

dfdf

Program Source

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.geometry.Pos;
import javafx.stage.Modality;

class SubStage {
    
    SubStage(MainStage main)
    {
        Stage subStage = new Stage();
        subStage.setTitle("SubStage");
        subStage.initModality(Modality.APPLICATION_MODAL);
                
        FlowPane root = new FlowPane();
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 300, 100);
        
        Button btn = new Button("100");
        btn.setOnAction(eve->{
            main.txt.setText("100");
            subStage.close();
        });
        
        Button btn2 = new Button("200");
        btn2.setOnAction(eve->{
            main.txt.setText("200");
            subStage.close();
        });
        
        Button btn3 = new Button("300");
        btn3.setOnAction(e->{
            main.txt.setText("300");
            subStage.close();
        });  
        
        root.getChildren().addAll(btn,btn2,btn3);
        subStage.setScene(scene);
        subStage.show();
    }
}

public class MainStage extends Application {
    
    TextField txt;
    
    public static void main(String[] args) {
        
        Application.launch(args);
    }
    
    public void start(Stage primaryStage) {
        
        primaryStage.setTitle("PrimaryStage");
        
        FlowPane root = new FlowPane();
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 700, 200);
        
        txt = new TextField();
        Button btn = new Button("Choose Value");
        btn.setOnAction(eve-> new SubStage(this));
            
        root.getChildren().addAll(txt,btn);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Leave a Comment