I attached the following sample application of javafx, in that there is a dialog class which is used to dialog box. While click on button exist on dialog box - it memory get raised too much. Simply when the dialog box shows - support in taskmanager it takes 57kb and then as we click on button and on dispose the dialog - taskmanager shows its memory start increasing - and finally it got crash, Getting dump memory exception.
There are following classes in the sample Dialog.java : it shows the dialog box with as ok - cancel button MessageDialog.fxml : this fxml create the the dialog MessageDialogController is associate file to MessageDialog.fxml JavaFXSample.java is the main class for runing this sample.
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;
/**
*
* @author Admin
*/
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);
// Utility.setCentreLocation(stage, parentStage);
InputStream inputStream = null;
try {
inputStream = Dialog.class.getResourceAsStream(image);
stage.getIcons().add(new Image(inputStream));
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
}
}
}
stage.showAndWait();
}
public static Initializable replaceScene(String fXml, Stage mystage) {
InputStream in = null;
try {
FXMLLoader loader = new FXMLLoader();
in = Dialog.class.getResourceAsStream(fXml);
loader.setLocation(Dialog.class.getResource(fXml));
loader.setBuilderFactory(new JavaFXBuilderFactory());
AnchorPane page;
try {
page = (AnchorPane) loader.load(in);
} finally {
in.close();
}
Scene scene = new Scene(page);
mystage.setScene(scene);
return loader.getController();
} catch (Exception ex) {
System.out.println("Exception in replaceScene. " + fXml + ".Error:" + ex.getMessage());
return null;
}
}
}
JavaFXSample.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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;
/**
*
* @author JavaUser1
*/
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();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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;
/**
* FXML Controller class
*
* @author Admin
*/
public class MessageDialogController implements Initializable {
@FXML
private Label lblMessage;
@FXML
private Label lblicon;
@FXML
private HBox hBox;
private Stage myStage;
private String clicked = "cancel";
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void btnOkEvent(ActionEvent event) {
clicked = "yes";
myStage.close();
}
public void btnCancelEvent(ActionEvent event) {
clicked = "no";
myStage.close();
}
public void init(Stage stage, String Message, String image) {
Button btnOk = new Button("OK");
btnOk.setPrefSize(70.0, 23.0);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
btnOkEvent(t);
}
});
hBox.getChildren().add(btnOk);
lblMessage.setText(Message);
if (image != null && !image.trim().isEmpty()) {
Image imageRunBackupPlan = new Image(getClass().getResourceAsStream(image));
lblicon.setGraphic(new ImageView(imageRunBackupPlan));
}
this.myStage = stage;
}
public void init(Stage stage, String Message, String btnOkText, String btnCancelText, String image) {
clicked = "cancel";
Button btnOk = new Button(btnOkText);
btnOk.setPrefHeight(23);
Button btnCancel = new Button(btnCancelText);
btnCancel.setPrefHeight(23);
hBox.getChildren().addAll(btnOk, btnCancel);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
btnOkEvent(t);
}
});
btnCancel.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
btnCancelEvent(t);
}
});
lblMessage.setText(Message);
if (image != null && !image.trim().isEmpty()) {
InputStream inputStream = null;
Image imageRunBackupPlan = new Image(getClass().getResourceAsStream(image));
lblicon.setGraphic(new ImageView(imageRunBackupPlan));
}
this.myStage = stage;
}
public String clickedOn() {
return clicked;
}
}
If the purpose of your Dialog.java is just to create a dialog box, then remove it and use this. It's just a jar file you have to import to your project
http://code.makery.ch/blog/javafx-2-dialogs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With