A Way For Learning

Java Fx-Communicating Between Windows

No comments
Confirmbox.java
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.*;
public class ConfirmBox {
//Create variable
static boolean answer;
public static boolean display(String title, String message) {
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(250);
Label label = new Label();
label.setText(message);
//Create two buttons
Button yesButton = new Button("Yes");
Button noButton = new Button("No");
//Clicking will set answer and close window
yesButton.setOnAction(e -> {
answer = true;
window.close();
});
noButton.setOnAction(e -> {
answer = false;
window.close();
});
VBox layout = new VBox(10);
//Add buttons
layout.getChildren().addAll(label, yesButton, noButton);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout);
window.setScene(scene);
window.showAndWait();
//Make sure to return answer
return answer;
}
} Main.java
mport javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
Button button;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("JavaFX - thenewboston");
button = new Button("Click Me");
button.setOnAction(e -> {
boolean result = ConfirmBox.display("Title of Window", "Are you sure you want to send that pic?");
System.out.println(result);
});
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
}

No comments :

Post a Comment