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

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
John Murano
  • 333
  • 1
  • 4
  • 8
  • 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. – Erick Robertson May 29 '15 at 12:51

7 Answers7

13

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.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
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);
ordnungswidrig
  • 3,140
  • 1
  • 18
  • 29
  • 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. – ensignr Oct 21 '16 at 14:06
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

Pooria
  • 780
  • 1
  • 8
  • 17
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 );
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1

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

Paki
  • 11
  • 1
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.

Alan
  • 716
  • 5
  • 15
-1
frame.setExtendedState( JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

Try the above..

Taryn
  • 242,637
  • 56
  • 362
  • 405
Kiran
  • 11