I'm running into a bit of a problem. I'm creating a program for a client. In the program, I have implement a dedicated 'close/shut down' button - which requires a password in order to properly shut down. But an alternative (and less safe) way of closing the program is by hitting the red close or 'X' button: top right (Windows) or top left(Mac).
I do not want the red x button to actually close the entire program. What I would like to know: is it possible to completely disable the red 'x' button from closing the entire program? If possible, could someone provide code for this?
What I'm using: IntelliJ IDEA (Ultimate), JavaFX with Java 8, Dev. Language: Java
Add a event handler to the onCloseRequest event of the stage. This allows you to prevent the window from closing by consuming the event and executing your own shutdown procedure instead:
private void shutdown(Stage mainWindow) {
// you could also use your logout window / whatever here instead
Alert alert = new Alert(Alert.AlertType.NONE, "Really close the stage?", ButtonType.YES, ButtonType.NO);
if (alert.showAndWait().orElse(ButtonType.NO) == ButtonType.YES) {
// you may need to close other windows or replace this with Platform.exit();
mainWindow.close();
}
}
#Override
public void start(Stage primaryStage) {
primaryStage.setOnCloseRequest(evt -> {
// prevent window from closing
evt.consume();
// execute own shutdown procedure
shutdown(primaryStage);
});
StackPane root = new StackPane();
Scene scene = new Scene(root, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
Related
I have a small application in JavaFX running on Windows 10, I've changed many things inside the application and now I am facing the problem that my InfoBox is showing up with header and Picture but no text is wiritten inside the box.
I am calling this inside a part of my app:
infoBox("Karte wurde gelesen... bitte warten ", "Information-NFC", "Information-NFC!");
and here is the function itself:
public void infoBox(String infoMessage, String titleBar, String headerMessage)
{
Alert alert = new Alert(AlertType.INFORMATION);
alert.setResizable(true);
alert.getDialogPane().setPrefSize(480, 320);
alert.getDialogPane().setStyle("-fx-text-fill: red;-fx-font-size: 15px; -fx-font: 10px Tahoma; ");
alert.setTitle(titleBar);
alert.setHeaderText(headerMessage);
alert.setContentText(infoMessage);
System.out.println(infoMessage+"here");
//Logo für Programm Fenster
//////////// Logo ProgrammFesnter end
Image img2 = new Image(getClass().getResourceAsStream("logo.PNG"));
Stage dialogStage = (Stage) alert.getDialogPane().getScene().getWindow();
dialogStage.getIcons().add(img2);
alert.show();
try {
Thread.sleep(8000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
alert.close();
}
I can see in the log window that the variable "infoMessage" is set but the Alert Box itself is empty.
And I don't know why.
Anyone of you out there had this problem before?
Everything is running fine except this small little box... ???
Thanks to all out there and have nice holidays....
Stev
Most likely the problem is the Thread.sleep(8000) call. That sleeps the JavaFX Application Thread which prevents user interaction with the UI and also prevents any render "pulses" from being scheduled. In other words, your application is unresponsive for those 8 seconds.
If you want to delay an action on the FX thread then you should use the animation API. And since you appear to want to wait for the alert to close before allowing the code to proceed you should also make use of the onShown property and showAndWait() method of Dialog. For example:
Alert alert = ...;
// configure alert
PauseTransition closeDelay = new PauseTransition(Duration.seconds(8));
closeDelay.setOnFinished(e -> alert.close());
alert.setOnShown(e -> closeDelay.playFromStart());
alert.showAndWait();
I have tic-tac-toe game on BorderPane. On the left side I have simple chat. I want set possibility send another message after first after 5 second, i.e. I want set disable for chat for 5 second, but another parts of application must work. How can I do this.
#FXML
private GridPane mainGridPane; //here game
#FXML
private GridPane smileGridPane; //this I want stop for five second
A PauseTransition with a onFinished handler reenabling the pane will do the trick:
smileGridPane.setDisable(true); // disable target pane
PauseTransition pause = new PauseTransition(Duration.seconds(5));
pause.setOnFinished(evt -> smileGridPane.setDisable(false)); // reenable target pane
pause.play();
my application consists of a TabPane named tabPane with, lets say, two Tabs: firstTab and secondTab. Whenever firstTab is selected, I want the application to be in FSEM (full-screen exclusive mode, see documentation); otherwise it should be in normal windowed mode.
My usual way of being notified about which Tab is active is either
BooleanBinding isFirstTabActive = tabPane.getSelectionModel().
selectedItemProperty().isEqualTo(firstTab);
or
tabPane.getSelectionModel.selectedItemProperty().addListener(...);
Since stage.fullScreenProperty() is read-only and therefore cannot be binded (as stated in docs), I can not use my first method and do, lets say,
stage.fullScreenProperty().bind(isFirstTabActive) //not working.
Since I am not planning on sigining the application for now and, as the docs state:
Applications can only enter FSEM in response to user input. More specifically, entering is allowed from mouse (Node.mousePressed/mouseReleased/mouseClicked) or keyboard (Node.keyPressed/keyReleased/keyTyped) event handlers.
I can not use the second method either, by doing
tabPane.getSelectionModel.selectedItemProperty().addListener(new ChangeListener<Tab>() {
#Override
public void changed(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue) {
stage.setFullScreen(newValue == firstTab);
}
);
because this way the application does not enter FSEM but only emulated fullscreen mode.
I do know that I can enter FSEM by creating EventHandlers which listen to tabPane events. But this way I had to add two EventHandlers for both MouseEvent.MOUSE_CLICKED and KeyEvent.KEY_TYPED, and the EventHandlers would have to explicitly find out whats going on ignoring their Event, like:
tabPane.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
stage.setFullScreen(tabPane.getSelectionModel().getSelectedItem() == firstTab);
}
});
So is this really the way to go here? Or can I do some workaround with the Bindings? Any suggestions here? Thanks.
Hello i am making a web browser in javafx and i want to make it fullscreen when user clicks on Fullscreen button. I tried the below given code but unfortunately it is giving error.
Fullscreen.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.setFullScreen(true); // here it gives error "cannot find variable primaryStage"
}
});
**I know that this doesn't follow the basics of java but i didn't find another way of implementing it.
Kindly help :)
Assuming Fullscreen is a button, just get the stage it's in with
((Stage)Fullscreen.getScene().getWindow()).setFullScreen(true);
I have two layouts in JavaFX app. The first one contains table view whereas the second one is a simple dialog to input data. The problem is that I want to refresh data after closing the dialog. Now I have a button on the first layout which refreshes data:
data.removeAll(data);
loadDataToTable();
But I don't want to invoke methods shown above with the button but automatically right after closing the dialog. I don't know how to make this, let's say, connection between those controllers.
thanks in advance
The new Dialog, if I am not wrong must be a new Stage ! Let us consider the new Stage to be modifyStage. We can call the onSetCloseRequest of the new Stage and put your code in it.
modifyStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent paramT) {
data.removeAll(data);
loadDataToTable();
}
});