File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes requestFocus() is not working across applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "requestFocus() is not working across applications" Watch "requestFocus() is not working across applications" New topic
Author

requestFocus() is not working across applications

Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
What's wrong with this code? I get focusLost() events all right, and when I requestFocus(), I get focusGained().. except it is NOT gained, my caret is still in Notepad! How do I get my caret out of there?

Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
Update. By adding f.requestFocusInWindow(), I now get TWO carets! One in f, and the other, in my Notepad. The one in Notepad, undoubtedly, native; that's where the keyboard input goes.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
The focus methods are only used to change focus within components on a Window.

I have no idea what Notepad has to do with this. If you have notepad open in a window and then start your Java appliaction and show a dialog, then the dialog should have focus by default.

It looks like you are trying to prevent your dialog from losing focus if you then click on Notepad. This is a bad design you should never prevent a user from using another application.

There are methods like alwaysOnTop() which will make sure your dialog is always visible. Or there might be times you want to use the toFront() method to make the dialog behave like a notification popup.
Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
Rob Camick wrote:I have no idea what Notepad

Ah, but you do.

It looks like you are trying to prevent your dialog from losing focus if you then click on Notepad.


Exactly.

This is a bad design you should never prevent a user from using another application.

Might be more prudent to ask WHY I need this design, before rushing with a judgment. I am simulating an ATM machine for very computer-illiterate users who only have numeric keypad anyway, and if any 3rd party software pops up with a dialog, they currently have no recourse other than rebooting.

There are methods like alwaysOnTop() which will make sure your dialog is always visible. Or there might be times you want to use the toFront() method to make the dialog behave like a notification popup.


Thanks, I've tried, neither did it for me :-(

Any leads welcome.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

Jane Jukowsky wrote:I am simulating an ATM machine for very computer-illiterate users who only have numeric keypad anyway, and if any 3rd party software pops up with a dialog, they currently have no recourse other than rebooting.


As far as I know this is generally solved by configuring the operating system to not allow any other software except your application. (I think it's called "kiosk" mode.) As Rob suggests, trying to prevent other applications from taking the focus is directly opposed to what the operating system naturally does, so you'll find the operating system doesn't make it easy for any application to do it.

So you are better off trying to manage the environment rather than trying to hack it.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
Might be more prudent to ask WHY I need this design, before rushing with a judgment.


Actually it would be more prudent for you to state the actual requirement so we don't have to guess exactly what it is you are trying to do.
Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
Thank you Paul, I did not know about kiosk mode. It does not appear Windows supports that natively; a free Microsoft utility SteadyState seems to, but it does a lot more, such as hard drive protection, so I'd much rather just kept focus with a 1-liner if I could. Somehow requestFocus used to work in Java 1.1 (or 1.05) a decade ago, and somehow it stopped working, even in java.awt.Dialog, hmm.

So.. sorry about the offtopic, but are there superior alternatives to SteadyState, as far as kiosks go?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
Somehow requestFocus used to work in Java 1.1 (or 1.05) a decade ago


The focus subsytem was rewritten for JDK1.3.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: requestFocus() is not working across applications
 
Similar Threads
JComboBox and FocusListener
Focus issues among JTextField and Dialogs
ask some questions about FocusListener
FocusListner not working on JMenu
In the swing of JFrame.setFocusableWindowState (false),Corresponds to what is in the method in swt