I have attached the following sample javafx application that has a dialog class that is used for a dialog box. Although the button has a button in the dialog box, the memory is too much increased. Just when the dialog box displays - support in the taskmanager, it takes 57kb, and then when we click on the button and insert the dialog box - the taskmanager shows that the beginning of its memory is increasing, and finally it got a failure, excluding memory .
The following Dialog.java classes are in the selection : it displays a dialog box with the ok button - cancel MessageDialog.fxml : this fxml creates the MessageDialogController dialog - the associated file with MessageDialog.fxml JavaFXSample.java - the main class to run this example.
Dialog.java
package javafxsample; import java.io.IOException; import java.io.InputStream; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.fxml.JavaFXBuilderFactory; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.layout.AnchorPane; import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.StageStyle; public class Dialog { public static void ShowinfoDialog(String title, String Message, Stage parentStage, double w, double h) { if (title == null || title.trim().isEmpty()) { title = "Info"; } showMessageDialog(title, Message, parentStage, "sidetheme.png", w, h); } public static void showMessageDialog(String title, String Message, Stage parentStage, String image, double w, double h) { Stage stage = new Stage(); MessageDialogController messageDialogController = (MessageDialogController) replaceScene("/javafxsample/MessageDialog.fxml", stage); messageDialogController.init(stage, Message, image); if (parentStage != null) { stage.initOwner(parentStage); } stage.initModality(Modality.APPLICATION_MODAL); stage.initStyle(StageStyle.UTILITY); stage.setResizable(false); if (title != null && !title.trim().isEmpty()) { stage.setTitle(title); } stage.setWidth(w); stage.setHeight(h);
JavaFXSample.java
package javafxsample; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class JavaFXSample extends Application { @Override public void start(final Stage primaryStage) { Button btn = new Button(); btn.setText(" Click on ME "); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { Dialog.ShowinfoDialog("Sample", "Clicked on button", primaryStage, 400.0, 150.0); } }); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
MessageDialog.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.geometry.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <AnchorPane id="AnchorPane" prefHeight="138.0" prefWidth="306.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxsample.MessageDialogController"> <children> <TitledPane animated="false" collapsible="false" prefHeight="138.0" prefWidth="347.0" text="" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="-40.0"> <content> <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <GridPane prefHeight="112.0" prefWidth="282.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <children> <Label fx:id="lblicon" text="" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0"> <GridPane.margin> <Insets top="20.0" fx:id="x1" /> </GridPane.margin> </Label> <Label fx:id="lblMessage" text="" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="0"> <font> <Font size="14.0" /> </font> <GridPane.margin> <Insets left="2.0" top="20.0" /> </GridPane.margin> </Label> <Separator prefWidth="200.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="1" /> <Separator prefWidth="200.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="1"> <GridPane.margin> <Insets top="4.0" /> </GridPane.margin> </Separator> <HBox id="HBox" fx:id="hBox" alignment="CENTER" spacing="5.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.halignment="CENTER" GridPane.rowIndex="2" /> </children> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" maxWidth="82.0" minWidth="82.0" prefWidth="82.0" /> <ColumnConstraints hgrow="SOMETIMES" maxWidth="-1.0" minWidth="10.0" prefWidth="220.0" /> </columnConstraints> <rowConstraints> <RowConstraints maxHeight="-1.0" minHeight="10.0" prefHeight="68.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="15.0" minHeight="15.0" prefHeight="15.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="29.0" minHeight="29.0" prefHeight="29.0" vgrow="SOMETIMES" /> </rowConstraints> </GridPane> </children> </AnchorPane> </content> </TitledPane> </children> </AnchorPane>
MessageDialogController.java
package javafxsample; import java.io.InputStream; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class MessageDialogController implements Initializable { @FXML private Label lblMessage; @FXML private Label lblicon; @FXML private HBox hBox; private Stage myStage; private String clicked = "cancel"; @Override public void initialize(URL url, ResourceBundle rb) {