How to focus a JFrame?
Asked Answered
F

7

11

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

Freemasonry answered 13/3, 2009 at 1:36 Comment(1)
To future visitors who want to focus a JFrame, please see my answer down the page which uses frame.requestFocus(). The accepted answer solves OP's problem, but the solution is very specific to his situation.Behlau
J
4

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 );
Jug answered 13/3, 2009 at 1:47 Comment(1)
While this solution does solve the OP's problem, it does not answer the title question.Behlau
B
16

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.

Behlau answered 29/5, 2015 at 12:29 Comment(0)
B
11

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);
Bozarth answered 13/3, 2009 at 14:37 Comment(1)
Thanks. This worked for me, with the caveat that I had to call them (immediately) after frame.setVisible(true). I guess that makes sense, but I was doing them separately.Jackfruit
P
4

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

Panhandle answered 13/3, 2009 at 1:45 Comment(1)
I tried this, and this method does not work. Thanks anyway, though!Freemasonry
J
4

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 );
Jug answered 13/3, 2009 at 1:47 Comment(1)
While this solution does solve the OP's problem, it does not answer the title question.Behlau
U
1

Just add the following line of code above the JOptionPaneMessageDialog code ... this.setAlwaysOnTop(false);

Ulrick answered 8/11, 2016 at 20:12 Comment(0)
T
0

toFront () worked for me in a similar situation where I wanted a JFrame to create another JFrame but have the first JFrame get or retain focus. The new JFrame was always getting the focus.

requestFocus () didn't work.
requestFocusInWindow () didn't work.
Various calls to one or the other above two methods didn't work, be it from a JFrame or a GlassPane or a getContentPane () or a JPanel().
setAlwaysOnTop (true) didn't work.

Inside a JFrame subclass:

   public void configure () {
      if (glassPane == null) {
         return;
      }
      final boolean vis = ! glassPane.isVisible (); // Toggle
      glassPane.setVisible (vis);
      if (vis) {
         configFrame = new ConfigFrame ("Configuration", props, glassPane, this);
         configFrame.addWindowListener (new AdapterForClosingSubs (configureToggle));
         toFront ();
      } else {
         configFrame.setVisible (false);
         configFrame.dispose ();
         configFrame = null;
      }
      if (getFocusOwner () != null) {
         System.out.println ("configure focus owner " + getFocusOwner ().getName ());
      }
   }


   private final Runnable configureToggle = () -> {
      configure ();
   };

fwiw, Java 17.

Trimolecular answered 5/11, 2022 at 0:56 Comment(0)
J
-1
frame.setExtendedState( JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

Try the above..

Jalap answered 11/7, 2012 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.