Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory raise on dialog button click in javafx application
    text
    copied!<p>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.</p> <p>There are following classes in the sample <strong>Dialog.java</strong> : it shows the dialog box with as ok - cancel button <strong>MessageDialog.fxml</strong> : this fxml create the the dialog <strong>MessageDialogController</strong> is associate file to MessageDialog.fxml <strong>JavaFXSample.java</strong> is the main class for runing this sample.</p> <p><strong>Dialog.java</strong></p> <pre><code>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 &amp;&amp; !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; } } } </code></pre> <p><strong>JavaFXSample.java</strong></p> <pre><code> /* * 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&lt;ActionEvent&gt;() { @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); } } </code></pre> <p><strong>MessageDialog.fxml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;?import java.lang.*?&gt; &lt;?import java.util.*?&gt; &lt;?import javafx.geometry.*?&gt; &lt;?import javafx.scene.*?&gt; &lt;?import javafx.scene.control.*?&gt; &lt;?import javafx.scene.layout.*?&gt; &lt;?import javafx.scene.text.*?&gt; &lt;AnchorPane id="AnchorPane" prefHeight="138.0" prefWidth="306.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxsample.MessageDialogController"&gt; &lt;children&gt; &lt;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"&gt; &lt;content&gt; &lt;AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"&gt; &lt;children&gt; &lt;GridPane prefHeight="112.0" prefWidth="282.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt; &lt;children&gt; &lt;Label fx:id="lblicon" text="" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0"&gt; &lt;GridPane.margin&gt; &lt;Insets top="20.0" fx:id="x1" /&gt; &lt;/GridPane.margin&gt; &lt;/Label&gt; &lt;Label fx:id="lblMessage" text="" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="0"&gt; &lt;font&gt; &lt;Font size="14.0" /&gt; &lt;/font&gt; &lt;GridPane.margin&gt; &lt;Insets left="2.0" top="20.0" /&gt; &lt;/GridPane.margin&gt; &lt;/Label&gt; &lt;Separator prefWidth="200.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="1" /&gt; &lt;Separator prefWidth="200.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="1"&gt; &lt;GridPane.margin&gt; &lt;Insets top="4.0" /&gt; &lt;/GridPane.margin&gt; &lt;/Separator&gt; &lt;HBox id="HBox" fx:id="hBox" alignment="CENTER" spacing="5.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.halignment="CENTER" GridPane.rowIndex="2" /&gt; &lt;/children&gt; &lt;columnConstraints&gt; &lt;ColumnConstraints hgrow="SOMETIMES" maxWidth="82.0" minWidth="82.0" prefWidth="82.0" /&gt; &lt;ColumnConstraints hgrow="SOMETIMES" maxWidth="-1.0" minWidth="10.0" prefWidth="220.0" /&gt; &lt;/columnConstraints&gt; &lt;rowConstraints&gt; &lt;RowConstraints maxHeight="-1.0" minHeight="10.0" prefHeight="68.0" vgrow="SOMETIMES" /&gt; &lt;RowConstraints maxHeight="15.0" minHeight="15.0" prefHeight="15.0" vgrow="SOMETIMES" /&gt; &lt;RowConstraints maxHeight="29.0" minHeight="29.0" prefHeight="29.0" vgrow="SOMETIMES" /&gt; &lt;/rowConstraints&gt; &lt;/GridPane&gt; &lt;/children&gt; &lt;/AnchorPane&gt; &lt;/content&gt; &lt;/TitledPane&gt; &lt;/children&gt; &lt;/AnchorPane&gt; </code></pre> <p><strong>MessageDialogController.java</strong></p> <pre><code>/* * 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&lt;ActionEvent&gt;() { @Override public void handle(ActionEvent t) { btnOkEvent(t); } }); hBox.getChildren().add(btnOk); lblMessage.setText(Message); if (image != null &amp;&amp; !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&lt;ActionEvent&gt;() { @Override public void handle(ActionEvent t) { btnOkEvent(t); } }); btnCancel.setOnAction(new EventHandler&lt;ActionEvent&gt;() { @Override public void handle(ActionEvent t) { btnCancelEvent(t); } }); lblMessage.setText(Message); if (image != null &amp;&amp; !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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload