79811

JavaFX closing application modal dialog

Question:

I'm using <a href="https://stackoverflow.com/questions/14168064/how-to-create-a-javafx-dialog" rel="nofollow">this</a> example to create application modal dialog. When I click exit button on my dialog (red one in top right corner) everything works fine. Dialog gets closed and then I can open it normaly. But when I try to add a Button which closes my dialog, everything works fine until I try to reopen it. After that, it throws me a IllegalStateException (I'll update answer with this exception if needed).

This is an event handler which demonstrates how I close a dialog:

btnClose.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { dialog.close(); } });

Can someone tell me how to properly close application modal dialog? Thanks in advance.

Answer1:

<em>Edit</em>

I see you found your issue, guess I just keep my answer with the sample code in case somebody else has a similar issue.

<hr />

Your btnClose action works for me, so the issue is probably in some code which you have not posted.

Here is a sample I created to test it:

import javafx.application.Application; import javafx.event.*; import javafx.geometry.Pos; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class DialogClosing extends Application { @Override public void start(final Stage stage) { final Button showDialog = new Button("Show Dialog"); showDialog.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { showDialog(stage, showDialog); } }); StackPane layout = new StackPane(); layout.getChildren().setAll( showDialog ); layout.setStyle("-fx-padding: 10px;"); stage.setScene( new Scene( layout ) ); stage.show(); } private Stage showDialog(Window parent, final Node showControlNode) { showControlNode.setDisable(true); final Stage dialog = new Stage(); dialog.initOwner(parent); dialog.initStyle(StageStyle.UTILITY); dialog.setX(parent.getX()); dialog.setY(parent.getY() + parent.getHeight()); Button closeDialog = new Button("Close Dialog"); closeDialog.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { dialog.close(); } }); dialog.setOnHidden(new EventHandler<WindowEvent>() { @Override public void handle(WindowEvent windowEvent) { showControlNode.setDisable(false); } }); VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER); layout.getChildren().addAll( new Label("Hello World!"), closeDialog ); layout.setStyle("-fx-padding: 10px;"); Scene scene = new Scene( layout, 125, 100 ); dialog.setScene(scene); dialog.show(); return dialog; } public static void main(String[] args) { launch(args); } }

<img alt="closingtimeoutput" class="b-lazy" data-src="https://i.stack.imgur.com/xskGX.png" data-original="https://i.stack.imgur.com/xskGX.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer2:

Ok, nevermind. Things fixed. Sorry for inconvenience. The problem was I declared my Stage as static.

Recommend

  • JavaFx: Pass Value to another class and update Label text
  • javafx change style of a node frequently not by CSS
  • Assigning an action to each button, in an array of buttons in JavaFX
  • Visual Studio 2015 Using Incorrect Cordova Version
  • Search feature with multiple criteria - PHP/MySQL
  • Julia custom type assignment
  • Python alternative for calculating pairwise distance between two sets of 2d points [duplicate]
  • How to store data through web service when data coming to my serial port will be asynchronous?
  • Neo4J / py2neo — cursor-based query?
  • Change behaviour of Print button in ReportViewer C#
  • Create an Event Handler whenever a new Node is added in treeview
  • Asynchronous Sockets - Handling false socket.AcceptAsync values
  • Lua: Line breaks in strings
  • netsh acl setting (need alternative method - registry settings?)
  • how to set variables in a php include file?
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • D3 nodes and links from JSON with nested arrays of children
  • Django rest serializer Breaks when data exists
  • How to rebase a series of branches?
  • Recording logins for password protected directories
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Azure Cloud Service Web Role web pages do not load
  • sending/ receiving email in Java
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • what is the difference between the asp.net mvc application and asp.net web application
  • R: gsub and capture
  • How to format a variable of double type
  • Cannot Parse HTML Data Using Android / JSOUP
  • Data Validation Drop Down Box Arrow Disappearing
  • Benchmarking RAM performance - UWP and C#
  • JTable with a ScrollPane misbehaving
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?