Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to focus a JFrame?

I am writing a small game, with one JFrame that holds the main game, and another JFrame that displays the score. the problem is, when I am done constructing them, the score JFrame always ends up focused! I have tried calling scoreDisplay.toFront(), scoreDisplay.requestFocus(), and even:

display.setState(JFrame.ICONIZED);
display.setState(JFrame.NORMAL);

Is there any way to make this work? Thanks in advance, john murano

like image 215
John Murano Avatar asked Mar 13 '09 01:03

John Murano


People also ask

What is focus in Java?

The current GUI element that is "active" has the focus. For example when you have several Input windows only one can have the focus and receive your keyboard input. See here the Android GUI doc http://developer.android.com/guide/topics/ui/ui-events.html. Follow this answer to receive notifications.

What does frame setVisible true do?

The setVisible(true) method makes the frame appear on the screen. If you forget to do this, the frame object will exist as an object in memory, but no picture will appear on the screen.


4 Answers

Call the requestFocus() method.

This is not guaranteed to work, because there are many reasons why an operating system would not allow a frame to have focus. There could be another frame with higher priority in a different application. There are also some linux desktops which (if I recall correctly) do not allow frames to request focus.

To give you a better chance of success, I also recommend calling the toFront() method before requesting focus.

frame.setVisible(true);
frame.toFront();
frame.requestFocus();

Please keep in mind, none of this is guaranteed because frame handling, especially with focus and layering, is very operating system-dependant. So set the frame to visible, move it to the front, and request the focus. Once you give up the EDT, the operating system will likely give the frame the focus. At the very least, the window should be on top.

like image 103
Erick Robertson Avatar answered Sep 21 '22 20:09

Erick Robertson


Toggle alwaysOnTop

See here:

http://forums.sun.com/thread.jspa?threadID=5124278

Read about toFront in the API http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html#toFront

Some platforms may not permit this VM to place its Windows above windows of native applications, or Windows of other VMs.

On Windows OS for example toFront causes the icon on the Task Bar to flicker, but the window stays in the back.

The only think that will force the window to front is setAlwaysOnTop.

frame.setAlwaysOnTop(true); 
frame.setAlwaysOnTop(false);
like image 35
ordnungswidrig Avatar answered Sep 19 '22 20:09

ordnungswidrig


Have you consider setting the score in the same frame as the game frame?

Other possible ( quick and dirty ) option is to create them in reverse order, or at least ( if score depends on game ) display them in reverse order.

score.setVisible( true );
game.setVisible( true );

My guess is that currently they are:

game.setVisible( true );
score.setVisible( true );
like image 20
OscarRyz Avatar answered Sep 21 '22 20:09

OscarRyz


The way that I would do is:

 frame.toFront();
 frame.setState(Frame.NORMAL); 

and If you also want have more control on it you should use requestFocuse.

BTW, here is an example : http://coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2006-06/msg00152.html

like image 33
Pooria Avatar answered Sep 20 '22 20:09

Pooria