96

We have a Java application that needs to be brought to the foreground when a telecontrol mechanism activates something in the application.

In order to get this, we have realized in the called method of the class which represents the frame of our application (extension of a JFrame) following implementation:

setVisible(true);
toFront();

Under Windows XP, this works the first time it is called, on the second time only the tab in the taskbar flashes, the frame doesn't come to the front anymore. Same goes for Win2k. On Vista it seems to work fine.

Do you have any ideas?

4
  • do you have a sample for this behavior?
    – OscarRyz
    Dec 22, 2008 at 21:20
  • 3
    The proper answer is to call toFront() on the EDT using invokeLater. There is a simple answer included below, but it is not the accepted answer. It does work, though. Perfectly. Mar 27, 2011 at 1:15
  • I know this is old, but this also happens on OSX
    – ferdil
    Jan 10, 2012 at 11:09
  • I'm experiencing this problem, but none of the answers below seems to solve it. I'm sure it's caused by windows not allowing me to 'Steal' Focus for my first window in the application. Jan 25, 2012 at 16:37

12 Answers 12

73

A possible solution is:

java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        myFrame.toFront();
        myFrame.repaint();
    }
});
4
  • 8
    Perhaps one should start ALL the UI code inside invokeLater in first place? ;) Aug 15, 2011 at 9:12
  • 2
    Did not work for me in Java 7 on KDE 4.9.5, the window would still hide below other programs. What helped me was changing the order of bringing windows to the front. Instead of hiding one window and show the second window, show the second window and then hide the first window (JFrame).
    – Lekensteyn
    Jan 12, 2013 at 23:23
  • 1
    Works with Windows 10 running Java 1.8 in an applet
    – Elliott
    Apr 13, 2016 at 20:57
  • What would be the inverse method? Feb 25, 2018 at 2:12
38

I had the same problem with bringing a JFrame to the front under Ubuntu (Java 1.6.0_10). And the only way I could resolve it is by providing a WindowListener. Specifically, I had to set my JFrame to always stay on top whenever toFront() is invoked, and provide windowDeactivated event handler to setAlwaysOnTop(false).


So, here is the code that could be placed into a base JFrame, which is used to derive all application frames.

@Override
public void setVisible(final boolean visible) {
  // make sure that frame is marked as not disposed if it is asked to be visible
  if (visible) {
      setDisposed(false);
  }
  // let's handle visibility...
  if (!visible || !isVisible()) { // have to check this condition simply because super.setVisible(true) invokes toFront if frame was already visible
      super.setVisible(visible);
  }
  // ...and bring frame to the front.. in a strange and weird way
  if (visible) {
      toFront();
  }
}

@Override
public void toFront() {
  super.setVisible(true);
  int state = super.getExtendedState();
  state &= ~JFrame.ICONIFIED;
  super.setExtendedState(state);
  super.setAlwaysOnTop(true);
  super.toFront();
  super.requestFocus();
  super.setAlwaysOnTop(false);
}

Whenever your frame should be displayed or brought to front call frame.setVisible(true).

Since I moved to Ubuntu 9.04 there seems to be no need in having a WindowListener for invoking super.setAlwaysOnTop(false) -- as can be observed; this code was moved to the methods toFront() and setVisible().

Please note that method setVisible() should always be invoked on EDT.

5
  • Thanks! Also related is this question: stackoverflow.com/questions/2315560/…
    – rogerdpack
    Aug 18, 2010 at 21:48
  • It doesn't compile by me because of setDisposed() method. Can't be found.
    – ka3ak
    Mar 16, 2013 at 17:23
  • 1
    @ka3ak This is a protected setter that could be introduced in the suggested JFrame-base class in order to track the situation with frame being disposed. Method dispose() would need to be overridden with a call to setDisposed(true). This is not strictly speaking needed for everyone.
    – 01es
    Mar 16, 2013 at 21:16
  • 1
    The .setAlwaysOnTop(true); was the only one that worked for me when using a JWindow.
    – DGolberg
    Feb 13, 2014 at 19:26
  • setAlwaysOnTop(true) is the only way I get it runing under windows 10 - thanks! Oct 25, 2018 at 9:33
23

Windows has the facility to prevent windows from stealing focus; instead it flashes the taskbar icon. In XP it's on by default (the only place I've seen to change it is using TweakUI, but there is a registry setting somewhere). In Vista they may have changed the default and/or exposed it as a user accessible setting with the out-of-the-box UI.

Preventing windows from forcing themselves to the front and taking focus is a feature since Windows 2K (and I, for one, am thankful for it).

That said, I have a little Java app I use to remind me to record my activities while working, and it makes itself the active window every 30 minutes (configurable, of course). It always works consistently under Windows XP and never flashes the title bar window. It uses the following code, called in the UI thread as a result of a timer event firing:

if(getState()!=Frame.NORMAL) { setState(Frame.NORMAL); }
toFront();
repaint();

(the first line restores if minimized... actually it would restore it if maximized too, but I never have it so).

While I usually have this app minimized, quite often it's simply behind my text editor. And, like I said, it always works.

I do have an idea on what your problem could be - perhaps you have a race condition with the setVisible() call. toFront() may not be valid unless the window is actually displayed when it is called; I have had this problem with requestFocus() before. You may need to put the toFront() call in a UI listener on a window activated event.

2014-09-07: At some point in time the above code stopped working, perhaps at Java 6 or 7. After some investigation and experimentation I had to update the code to override the window's toFront method do this (in conjunction with modified code from what is above):

setVisible(true);
toFront();
requestFocus();
repaint();

...

public @Override void toFront() {
    int sta = super.getExtendedState() & ~JFrame.ICONIFIED & JFrame.NORMAL;

    super.setExtendedState(sta);
    super.setAlwaysOnTop(true);
    super.toFront();
    super.requestFocus();
    super.setAlwaysOnTop(false);
}

As of Java 8_20, this code seems to be working fine.

6
  • 1
    +1 for supporting not allowing windows to steal the focus. I hate when that happens when I'm typing in a document.
    – Ken Paul
    Nov 23, 2008 at 2:00
  • 1
    I completely agree with you against stealing focus, but in this precise case the user expects the application to come to the front. But it would uncool to change the registry settings and change the complete windows behavior.
    – boutta
    Nov 24, 2008 at 7:30
  • I am guessing the super.setAlwaysOnTop(false); is so that the window isn't always on top, which is necessary to get rid of the true we set earlier to bring the window to the front, correct? I'm asking because with your code the window is still always on top in my case, which I obviously don't want. Running jre1.8.0_66 on Windows 10. Jan 6, 2016 at 12:20
  • @Bram: Yes that's correct. I'm running the code on the same version of Java and Windows and it does not end up always on top of other windows. It may not be necessary to set always on top, but I think otherwise Windows just flashes the title bar, at least under some conditions. Jan 9, 2016 at 1:43
  • Hm, strange. Could you take a look at a similar question in which I link to this answer? Maybe that code shows more clearly the issue: stackoverflow.com/questions/34637597/… Jan 9, 2016 at 12:12
12

Here's a method that REALLY works (tested on Windows Vista) :D

   frame.setExtendedState(JFrame.ICONIFIED);
   frame.setExtendedState(fullscreen ? JFrame.MAXIMIZED_BOTH : JFrame.NORMAL);

The fullscreen variable indicates if you want the app to run full screen or windowed.

This does not flash the task bar, but bring the window to front reliably.

2
  • Thanks for the setExtendedState tip. I used it along with the toFront() and repaint() solution to bring the window to the foreground even if it was minimized.
    – rob
    Nov 24, 2011 at 0:16
  • 1
    Confirmed: this solution works in WindowsXP, using toFront results in flashing message in task bar. Thanks! May 22, 2012 at 2:20
5

Hj, all methods of yours are not working for me, in Fedora KDE 14. I have a dirty way to do bring a window to front, while we're waiting for Oracle to fix this issue.

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class FrameMain extends javax.swing.JFrame {

  //...
  private final javax.swing.JFrame mainFrame = this;

  private void toggleVisible() {
    setVisible(!isVisible());
    if (isVisible()) {
      toFront();
      requestFocus();
      setAlwaysOnTop(true);
      try {
        //remember the last location of mouse
        final Point oldMouseLocation = MouseInfo.getPointerInfo().getLocation();

        //simulate a mouse click on title bar of window
        Robot robot = new Robot();
        robot.mouseMove(mainFrame.getX() + 100, mainFrame.getY() + 5);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

        //move mouse to old location
        robot.mouseMove((int) oldMouseLocation.getX(), (int) oldMouseLocation.getY());
      } catch (Exception ex) {
        //just ignore exception, or you can handle it as you want
      } finally {
        setAlwaysOnTop(false);
      }
    }
  }

  //...

}

And, this works perfectly in my Fedora KDE 14 :-)

2
  • A little hacky, works for us, but only for the first call :-). (Kubuntu 12.04) - other solution did fail
    – user85155
    Sep 21, 2012 at 16:40
  • This was the only solution that worked for me (Windows Server 2012 R2) for an issue where a JFrame (login) is opened but doesn't have focus until the user clicks on it.
    – glenneroo
    May 17, 2016 at 15:36
5

I tested your answers and only Stefan Reich's one worked for me. Although I couldn't manage to restore the window to its previous state (maximized/normal). I found this mutation better:

view.setState(java.awt.Frame.ICONIFIED);
view.setState(java.awt.Frame.NORMAL);

That is setState instead of setExtendedState.

4

This simple method worked for me perfectly in Windows 7:

    private void BringToFront() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                if(jFrame != null) {
                    jFrame.toFront();
                    jFrame.repaint();
                }
            }
        });
    }
1
  • 2
    The repaint() is not necessary, the invokeLater() did it. Thank you.
    – Matthieu
    Aug 24, 2015 at 13:25
3

Simplest way I've found that doesn't have inconsistency across platforms:

setVisible(false); setVisible(true);

3
  • 1
    causes some blinking though doesn't it? nice and simple though :)
    – rogerdpack
    Jul 12, 2011 at 16:39
  • did not work for my background process. Also window comes up white for the first refresh if called from foreground process. Can't use for screen grabs.
    – DragonLord
    Jan 23, 2014 at 22:42
  • blinking can be avoided by checking if the window is iconified or not
    – totaam
    Nov 2, 2014 at 10:08
2

The rules governing what happens when you .toFront() a JFrame are the same in windows and in linux :

-> if a window of the existing application is currently the focused window, then focus swaps to the requested window -> if not, the window merely flashes in the taskbar

BUT :

-> new windows automatically get focus

So let's exploit this ! You want to bring a window to the front, how to do it ? Well :

  1. Create an empty non-purpose window
  2. Show it
  3. Wait for it to show up on screen (setVisible does that)
  4. When shown, request focus for the window you actually want to bring the focus to
  5. hide the empty window, destroy it

Or, in java code :

// unminimize if necessary
this.setExtendedState(this.getExtendedState() & ~JFrame.ICONIFIED);

// don't blame me, blame my upbringing
// or better yet, blame java !
final JFrame newFrame = new JFrame();
newFrame.add(new JLabel("boembabies, is this in front ?"));

newFrame.pack();
newFrame.setVisible(true);
newFrame.toFront();

this.toFront();
this.requestFocus();

// I'm not 100% positive invokeLater is necessary, but it seems to be on
// WinXP. I'd be lying if I said I understand why
SwingUtilities.invokeLater(new Runnable() {
  @Override public void run() {
    newFrame.setVisible(false);
  }
});
2
  • Didn't work on Win7, both windows flash (if I don't hide the 2nd).
    – NateS
    Aug 16, 2013 at 22:51
  • Creative. Didn't work for my background process on Win7, when covered. New frame does not come up on top. Older JDK 6u21.
    – DragonLord
    Jan 23, 2014 at 22:47
1

To avoid the window losing focus when its returning to visible after being hidden all that is needed is:

setExtendedState(JFrame.NORMAL);

Like so:

defaultItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showWindow();
                setExtendedState(JFrame.NORMAL);
            }
});
0

There are numerous caveats in the javadoc for the toFront() method which may be causing your problem.

But I'll take a guess anyway, when "only the tab in the taskbar flashes", has the application been minimized? If so the following line from the javadoc may apply:

"If this Window is visible, brings this Window to the front and may make it the focused Window."

0

Ran into a problem with the iconified/restore approach on Windows when the application has multiple JFrames. In this case, I found that hiding the other JFrames, then forcing the window to the front, then unhiding the JFrames did the trick.

    private static void bringFrameToTop(Frame topFrame) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            
            List<Frame> framesToShow = new ArrayList<>();
            
            for(Frame f : Frame.getFrames()) {
                if (f != topFrame && f.isShowing()) {
                    framesToShow.add(f);
                    f.setVisible(false);
                }
            }

            // Force our dialog to the front
            int origState = topFrame.getExtendedState();
            topFrame.setExtendedState(JFrame.ICONIFIED);
            topFrame.setExtendedState(origState);
            
// these don't appear to help anything              
//              topFrame.toFront();
//              topFrame.repaint();

            for(Frame f : framesToShow) {
                f.setVisible(true);
            }
            
            topFrame.toFront();
        }
    });
    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.