Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Main JFrame doesn't always regain focus when modal dialog closes  RSS feed

 
Simon George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm new to this forum so hopefully this is the right place to put this question.

I have a relatively simple GUI application written in Java 6u20 that involves a main gui window (extends JFrame). Most of the file menu operations result in a modal dialog (either JFileChooser or JOptionPane) being presented to the user.

The problem is that when running on Unix (HPUX), the process of changing focus to one of these modal dialogs occasionally results in a quick flash of the background terminal window (not necessarily that used to launch the Java), and the process of closing the modal dialogs (by any means, i.e. any dialog button or hitting esc) doesn't always return focus to the main extended JFrame object, sometimes it goes to the terminal window and sometimes just flashes the terminal window before returning to the main JFrame window.

I think the problem is with the Unix window manager deciding that the main focus should be a terminal window, not my Java application, since the problem occurs in both directions (i.e. focus from main JFrame to modal dialog or vice versa).

In most cases of JOptionPane, I DO specify that the main extended JFrame object is the parent.

I've tried multiple things, including calling the following methods:
.toFront();
.requestFocus();
.setAlwaysOnTop(true);
.setVisible(true);

just before and after dialog display, and following selection of any button from the dialogs. This reduced frequency of the problem, but it always tends to flash the terminal window if not return focus to it completely.

Any help appreciated thanks,
Simon

Self-contained compilable example below, but note that the problem doesn't occur running on Windows (XP) and that my actual program doesn't use auto-generated code generated by a guibuilder:

 
Mike Peters
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is fine. Try to start it in the background e.g. 'java example.Main &'. I do not experience the same problem on my system Debian Lenny.
 
Simon George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

We're currently launching it using a .ksh script, and tried using & notation in the call to that script.

I'll try it and let you know, hopefully it'll work, although it wasn't returning focus to the terminal window from which it was called, just the last focused window!

Simon
 
Simon George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Ok I tried the '&' notation at the end of the line, in both the k-script and when calling on the command line.

The program launched but the problem was still there :-(

Any other ideas?
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you altering focus anywhere else?
 
Simon George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the problem first happened, I was never setting focus.

Since then, I've included multiple calls to a method which does:
toFront();
requestFocus();
setAlwaysOnTop(true);
setVisible(true);

on the main window, which is referred to as a public static class variable.

Since including these calls, the problem has got "less", but it is definitely still present and still apparently random as to when it happens.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may not be a Java problem but a window manager problem. If that's the case you may not be able to solve it.

One annoyance I have in Windows is that when a non-active Java program shows a dialog (like a progress dialog or something) then this dialog will become the active window. I haven't been able to solve this either, since it's a Windows issue.
 
Simon George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My gut feeling is that it's a Unix windows manager problem, in which case does anyone know any options that can be set in Unix to change what priority/scope a program is given?
 
Simon George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your prompt replies and help!

After reading the 6262392 bug, I decided to re-write in java version 5 (luckily all I had to change was the LayoutManager), and the problem doesn't seem to be there any more.

Incidentally, my html-formatted message dialogs now include ">" characters at every newline (I used <br />); I assume this is because the html version is different or something, so I'll experiment to find something that works!

Cheers,
Simon
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon George wrote:Incidentally, my html-formatted message dialogs now include ">" characters at every newline (I used <br />); I assume this is because the html version is different or something, so I'll experiment to find something that works!

Cheers,
Simon


I think you're not supposed to terminate the BR like you're doing. Just use <br> and see if that works.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!