'Creating a JavaFX Scene from Button click; Close the created Scene
I am learning JavaFX, and creating a scene that will be created and displayed to get user input. I created a VBox that holds different textfields and a button that I want to use to close the scene that was created and then store that information to use elsewhere. I can't seem to figure out how to add functionality to the button that will close the scene.
I've tried adding a EventHandler that will do it, but I can't seem to get the program to work.
part of Controller
public void drawNewClass(ActionEvent actionEvent) throws IOException {
ClassInstance classInstance = new ClassInstance();
TextField classDescription = new TextField("Enter Description");
TextField className = new TextField("Enter Class Name");
Button close = new Button("Submit");
close.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
TextField attributes = new TextField("Enter Variables (privacy type name, format)");
VBox secondaryLayout = new VBox();
secondaryLayout.getChildren().addAll(classDescription, className, attributes, close);
Scene secondScene = new Scene(secondaryLayout, 230, 100);
// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Add Class");
newWindow.setScene(secondScene);
// Set position of second window, related to primary window.
newWindow.setX(canvas.getLayoutX() + 200);
newWindow.setY(canvas.getLayoutY() + 100);
newWindow.showAndWait();
.FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.StackPane?>
<BorderPane xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.diagrambuilder.csc260w2022project2.diagramBuilderController"
prefHeight="500.0" prefWidth="500.0"
fx:id="root">
<top>
<HBox>
<Button text="Close" fx:id="closeButton" onAction="#closeApplication"/>
<Button text="New" onAction="#newCanvas"/>
<Button text="Save" onAction="#saveCanvas"/>
<Button text="Save As" onAction="#saveAsCanvas"/>
<Button text="Load" onAction="#loadCanvas"/>
<Button text="Export as Image" onAction="#exportCanvas"/>
</HBox>
</top>
<left>
<VBox>
<Button text="Draw New Class" fx:id="newClass" onAction="#drawNewClass"/>
<Button text="Add New Relationship" onAction="#drawNewRelationship"/>
</VBox>
</left>
<center>
<StackPane fx:id="stackPane" style="-fx-border-color: #000; -fx-border-width: 1px"
maxWidth="500" maxHeight="500">
<Canvas fx:id="canvas" width="${stackPane.width}" height="${stackPane.height}"/>
</StackPane>
</center>
</BorderPane>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
