B4J Question Bring JavaFX Form to front & activate it

Status
Not open for further replies.

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

I want to bring the JavaFX window on windows to front and activate it. I have figured out that there are 3 possible solutions:
  • use the toFront & requestFocus methods of the stage object
  • use JNA to access the native Windows functions
  • use the WinActivate method of the AutoIt library
In the attached example project (use the 5 seconds delay to hide the form manually) you can find the implementation for the first two solutions. I believe that AutoIt is oversized for my project. Moreover, the WinActivate method is based on the same Windows APIs (at least I think so :rolleyes:), so I didn't try this solution.
However, if you run the example project, you will see that the form remains in the background. What is really striking: the SetForegroundWindow method of the user32 instance returns false.

Btw.: When I use AutoIt and just call WinActivate("AnyTitle", "") everything works fine. I know I can create a command line tool, and use it with the Shell object. But this is much slower and wouldn't meet my requirements

Any help?
 

Attachments

  • BringToFront-Activate.zip
    1.1 KB · Views: 225

Daestrum

Expert
Licensed User
Longtime User
I ran your example and the form pops back up on top of the IDE.
The only difference is I use jna.4.2.1 and not 4.5.1, I seem to recall I had problems before using 4.5.1 so went back to 4.2.1
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

thank your for your answere.

I have tested it with version 4.2.1, but I got the same result.

(Just to be sure: When you have started the example App, you have 5 seconds to manually focus another Application. Only if the example App gets refocused the code works properly.)

Jan
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I ran the example in both debug and release.
After the form appears I clicked on the ide window, and the form window is hidden. After the delay, the window is back above the IDE.
The WinActivate and RequestFocus subs work exactly the same. The window will re-appear whichever one I use.
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Ok, that's weird.
On my PC (Windows 10.0.16299.125) the icon in the taskbar is just blinking with an orange background, but the form remains in the back.

Do you have another idea?

Jan
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I can re-create your problem as follows

If I change to a window that is from a program already in memory, it works as expected.

If I open a new program, that wasn't in memory before, then I get the flashing orange taskbar icon.
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
If I change to a window that is from a program already in memory, it works as expected.
This is not the case for me.
Just like you, I also used the B4J IDE during my tests, but I get the flashing icon every time.

Jan
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I found this, it may explain it (someone had a similar problem)
This is how the "toFront" functionality is implemented in Windows OS. Specifically: a process can be foreground or background. At step #3 your app is in the background, and Window won't allow it to bring its window to front - it will blink in the taskbar instead. However, once you've clicked a window belonging to the app, the app becomes a foreground process. Foreground processes in Windows are allowed to bring their windows to top of the stacking order. That's what you're observing at step #6.

This is not a bug in FX, so I'm closing this issue.
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
I found this, it may explain it (someone had a similar problem)
Interesting.
However, it doesn't explain why it worked on your pc but not on mine.
I don't like the solution, but for now it works: As my application is always on top, I simulate a mouse click on the form in order to activate the app.

Jan
 
Upvote 0
I am new to B4X so hopefully this works for you too. I tried many options (as described above and others) bu in the end what worked for me is the following:
B4X:
    MainForm.AlwaysOnTop = True
    MainForm.AlwaysOnTop = False

It is a bit hackish, but worked better that the other methods that I treid.
 
Upvote 0
Status
Not open for further replies.
Top