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?

Nathan
  • 8,093
  • 8
  • 50
  • 76
boutta
  • 24,189
  • 7
  • 34
  • 49
  • do you have a sample for this behavior? – OscarRyz Dec 22 '08 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. – Erick Robertson Mar 27 '11 at 01:15
  • I know this is old, but this also happens on OSX – ferdil Jan 10 '12 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. – Craig Warren Jan 25 '12 at 16:37

12 Answers12

73

A possible solution is:

java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        myFrame.toFront();
        myFrame.repaint();
    }
});
sth
  • 222,467
  • 53
  • 283
  • 367
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.

River
  • 8,585
  • 14
  • 54
  • 67
01es
  • 5,362
  • 1
  • 31
  • 40
  • Thanks! Also related is this question: http://stackoverflow.com/questions/2315560/how-do-you-force-a-java-swt-program-to-move-itself-to-the-foreground – rogerdpack Aug 18 '10 at 21:48
  • It doesn't compile by me because of setDisposed() method. Can't be found. – ka3ak Mar 16 '13 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 '13 at 21:16
  • 1
    The `.setAlwaysOnTop(true);` was the only one that worked for me when using a JWindow. – DGolberg Feb 13 '14 at 19:26
  • ```setAlwaysOnTop(true)``` is the only way I get it runing under windows 10 - thanks! – Hartmut Pfarr Oct 25 '18 at 09: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.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
  • 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 '08 at 02: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 '08 at 07: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. – Bram Vanroy Jan 06 '16 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. – Lawrence Dol Jan 09 '16 at 01: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: http://stackoverflow.com/questions/34637597/bring-jframe-window-to-the-front – Bram Vanroy Jan 09 '16 at 12:12
  • @Bram: Ahhh. I *have* seen that happen. My best guess is that if the current window receives user input between the time that the `toFront` is invoked and when it is actually displayed it will do what you describe; input focus remainson the window now behind it. I have that happen when I am coding. It's annoying at times because the new window *looks* like it has focus. I have even had the situation where key input is sent to *both* windows. What I've seen looks like a bug in Windows since it only started happening in Win 10 and appears to be an attempt to stop focus stealing, gone wrong. – Lawrence Dol Jan 11 '16 at 02:44
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.

Stefan Reich
  • 1,000
  • 9
  • 12
  • 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 '11 at 00:16
  • 1
    Confirmed: this solution works in WindowsXP, using toFront results in flashing message in task bar. Thanks! – Eric Lindauer May 22 '12 at 02: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 :-)

  • A little hacky, works for us, but only for the first call :-). (Kubuntu 12.04) - other solution did fail – user85155 Sep 21 '12 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 '16 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.

Community
  • 1
  • 1
Jarekczek
  • 7,456
  • 3
  • 46
  • 66
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();
                }
            }
        });
    }
Mr Ed
  • 5,068
  • 1
  • 19
  • 12
3

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

setVisible(false); setVisible(true);

Rich
  • 41
  • 1
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);
  }
});
christopher
  • 1,061
  • 2
  • 8
  • 10
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);
            }
});
Martin Sansone - MiOEE
  • 4,281
  • 1
  • 29
  • 31
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."

Brendan Cashman
  • 4,878
  • 2
  • 23
  • 20
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();
        }
    });
    
}
Kevin Day
  • 16,067
  • 8
  • 44
  • 68