Закрыть модальное окно java

How to close a java window with a button click — JavaFX Project

I made a JavaFX project and created the GUI for the first-login frame in the java Scene Builder. When the login is successful, the login frame must be closed and the next frame must be visible (main program frame). I can make the new frame appear, but I can’t make the login frame closed. I tried stuff like dispose() but nothing works. Below is the code for the main class:

public class KuberComm extends Application < @Override public void start(Stage stage) throws Exception < Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); Scene scene = new Scene(root); stage.setResizable(false); stage.setTitle("Login to KuberComm"); stage.setScene(scene); stage.show(); >/** * @param args the command line arguments */ public static void main(String[] args) < launch(args); >> 

The handler for the login button is located in another class (controller class made by the NetBeans IDE). I can’t figure out what the frame’s name is in order to use it in the controller class. Any help will be much appreciated!

5 Answers 5

give your button a name in the controller class:

@FXML public Button closeButton; 
@FXML public void handleCloseButtonAction(ActionEvent event)

In your FXML you need a reference to the button name and the method to call onAction:

This would close the stage that this button is on.

Источник

JavaFX closing application modal dialog

I’m using this 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() < @Override public void handle(ActionEvent event) < dialog.close(); >>); 

2 Answers 2

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

Читайте также:  Ftp file to server java

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() < @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() < @Override public void handle(ActionEvent actionEvent) < dialog.close(); >>); dialog.setOnHidden(new EventHandler() < @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); >> 

Источник

How to close the Modal using Selenium WebDriver?

I have a dropdown list, when I click on any of the dropdown value then a modal get opens, When I click outside the modal, it will get close. How to handle it with Selenium WebDriver? here is my Modal Code

 
You click on Dropdown hover Option
driver.switchTo().defaultContent(); 
driver.switchTo().alert().dismiss(); 

This isn’t a JavaScript Alert so you don’t need to use the switchTo() functionality. Just click on another element to dismiss it.

Doesn’t the Modal Dialog fade away automatically after certain time interval? Is clicking outside the modal necessary?

@Ardesco, I have tried to click on the other elements from the POM, but getting org.openqa.selenium.ElementClickInterceptedException

3 Answers 3

If you don’t want to hit the Esc key you can click on div that has been created to cover the rest of the page using the following:

 driver.get("https://demo.stqatools.com/MouseHover.php"); WebElement hoverButton = driver.findElement(By.cssSelector(".dropbtn")); WebElement linkOne = driver.findElement(By.cssSelector(".dropdown-content > a")); //Activate modal dialogue Actions action = new Actions(driver); action.moveToElement(hoverButton).perform(); wait.until(ExpectedConditions.visibilityOf(linkOne)); action.moveToElement(linkOne).click().perform(); //Dismiss modal dialogue driver.findElement(By.cssSelector(".show")).click(); 

Источник

Оцените статью