2

Obviously, this question already exists here. And over here someone answered it similarly. However, I tried both approaches (as I pointed out) and neither seems to work for me. I'm on Java 8 Update 22 on Windows 10, whereas the poster on that last post is not on Windows and on Java 7, so maybe one of these things has something to do with it. Code I have tried, where getInstance() is my JFrame object:

private static void bringToFront() {                                                             
    getInstance().setVisible(true);                                                              
    getInstance().setExtendedState(JFrame.NORMAL);                                               
    getInstance().toFront();                                                                     
    getInstance().repaint();                                                                     
}

And taken from this answer.

private static void bringToFront() {
    getInstance().setVisible(true);
    getInstance().toFront();
    getInstance().requestFocus();
    getInstance().repaint();
}

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

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

Note that I do not want the window to always stay on top. However, with that last snippet it does stay on top.

EDIT: I now realise it doesn't always stay on top. The problem is that it the window appears on top at first, but doesn't have focus (the icon in the task bar is flickering), meaning that if a user clicks somewhere else than the window the window will stay on top because it isn't "losing focus", as it didn't have any to begin with. I tried requestFocus() but that didn't work either. Probably because of this explanation. How can I make sure the window has focus on initial pop-up?


I tried mKorbel's solution, but that didn't work either. It works only when the window has already appeared and has been activated, and de-activated and when then the method is called, the window comes to the front. However, when first initialising the window, it doesn't show up to the front.

private static void bringToFront() {
    getInstance().setVisible(true);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            getInstance().toFront();
        }
    });
    getInstance().requestFocus();
    getInstance().repaint();
}
Community
  • 1
  • 1
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • 1
    again JFrame ignores toFront (based on peers from Native OS) this funcionality is reserved from JDialog/JWindow (with modality and alwaysInTop, etc), getExtendedState() & ~JFrame.ICONIFIED & JFrame.NORMAL; works for single instance of JFrame, then this container is moved to front on the screen (forward, over e.g. Chrome ...) – mKorbel Jan 06 '16 at 17:11
  • [you can to test](http://stackoverflow.com/a/6139696/714968), works for me from WinXP to Win10, incl. servers versions (2008/2012) with GUI – mKorbel Jan 06 '16 at 17:13
  • @mKorbel I really appreciate that you're trying to explain this to me, but I really don't understand what you're saying. From the example you linked to I only get that you use this on JFrame (which doesn't work for me; I can't use iconified -> normal, see my linked question): `frame.setExtendedState(Frame.ICONIFIED); frame.setExtendedState(Frame.NORMAL); frame.setAlwaysOnTop(true); frame.setAlwaysOnTop(false);` – Bram Vanroy Jan 06 '16 at 18:31
  • Focus is asynchronous, has to be wrapped into invokeLater, without flashing on the screen, occasionally flashing by using the timer – mKorbel Jan 07 '16 at 06:49
  • @mKorbel See my edit. I tried it as you suggested, but that doesn't work either. If this wasn't what you meant, please add an answer with the code you assume would work. Thank you. – Bram Vanroy Jan 07 '16 at 12:16
  • hmmm is there only one JFrame (already visible on the screen) – mKorbel Jan 07 '16 at 15:18
  • @mKorbel See my edit. It seems that the problem is that on initial pop u, the window doesn't have focus. – Bram Vanroy Jan 09 '16 at 13:34

2 Answers2

3

This is what I use below.

Warning: YMMV! I don't guarantee at all that it will work and if it does it might not work on another OS, a different version of the same OS, on your version of JAVA, or when Saturn aligns with Jupiter, etc etc.

Here goes:

private void hackyToFront( )
{
    // What follows is a hack to make sure that the frame is put to front and activated.
    // Simply calling setVisible( true ) and toFront( ) is not enough.

    SwingUtilities.invokeLater( new Runnable( )
    {
        @Override
        public void run( )
        {
            if( !isVisible( ) )
                setVisible( true );
            setExtendedState( JFrame.NORMAL );
            toFront( );
            setAlwaysOnTop( true );
            try
            {
                final Point oldMouseLocation = MouseInfo.getPointerInfo( ).getLocation( );

                // simulate a mouse click on title bar of window
                Robot robot = new Robot( );
                robot.mouseMove( getX( ) + 100, getY( ) + 10 );
                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 )
            {}
            finally
            {
                setAlwaysOnTop( false );
            }
        }
    } );
}
TT.
  • 15,774
  • 6
  • 47
  • 88
0

Possible solution:

private static void bringToFront() {                                                             
   getInstance().setVisible(true); 
   getInstance().setFocusable(true);
   getInstance().requestFocus();
}
Pendula
  • 688
  • 6
  • 17
  • As I am reading the rest of the comments, I think you will need to provide more of the code you have. The windows that you are trying to bring to front, do you ever close it? If yes, is setDefaultCloseOperation() been set? – Pendula Jan 07 '16 at 17:04