Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaFX, how to open another stage without losing focus of the first one?

Tags:

java

javafx

I have a main stage that must open another stage without losing focus of the first one. I know i can call mainWindow.requestFocus() after calling secondWindow.show() but i want to make it to work without the first window even losing focus.

I wanto to do this because the second stage is a notification window with StageStyle.TRANSPARENT, that stays always on top and closes itself after some seconds. Is there a way to make the second window "unfocusable"?

like image 566
Mateus Viccari Avatar asked Sep 18 '15 11:09

Mateus Viccari


People also ask

Can you have multiple stages displayed in JavaFX?

JavaFX Stage is a standalone application displaying window. Stage is primary element in JavaFX. We can add as many stage as we want but we have only one primary stage.

What is primary stage in JavaFX?

The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application. Stage objects must be constructed and modified on the JavaFX Application Thread.

What are the two parameters of stage in JavaFX divided as?

A stage has two parameters determining its position namely Width and Height. It is divided as Content Area and Decorations (Title Bar and Borders).


1 Answers

Do you really need to create a new Stage for showing your notification window? You could also use javafx.stage.Popup which creates transparent windows by default (so you would not need to set StageStyle.TRANSPARENT). Another advantage of using Popup instead of Stage is that it doesn't "steal" the focus from your main stage, wich should be pretty much what you need.

Here is some more information about the popup class: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Popup.html

And here is a simple example of how to use a popup in your application: https://gist.github.com/jewelsea/1926196

like image 191
Jan Gassen Avatar answered Oct 30 '22 14:10

Jan Gassen