3

I have a swing program which creates a fullscreen borderless window -- I am running on Windows 7. I need the program to be able to focus and bring itself to the front. However, when I attempt to use the method found here, How to bring a window to the front?, instead of bringing the window to the front the window just flashes in the taskbar and does not accept input. Below I wrote a small program that demonstrates the issue:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;


public class WindowTest extends JFrame{

WindowTest(){
    setTitle("Window Test");
    setSize(600, 600);
    setLocationRelativeTo(null);
    setUndecorated(true);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            final WindowTest wt = new WindowTest();
            wt.setVisible(true);

            Timer t = new Timer(3000,new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    java.awt.EventQueue.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            wt.toFront();
                            wt.repaint();
                        }
                    });             
                }   
            });

            t.setRepeats(false);
            t.start();

            wt.addKeyListener(new KeyListener(){
                @Override
                public void keyPressed(KeyEvent arg0) {
                    if(arg0.getKeyCode() == KeyEvent.VK_ESCAPE){
                        wt.dispose();
                        System.exit(0);
                        return;
                    }
                }

                @Override
                public void keyReleased(KeyEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void keyTyped(KeyEvent arg0) {
                    // TODO Auto-generated method stub

                }   
            });
        }
    });

}

 }

This will create a borderless, maximized window, and then three seconds later will attempt to bring it to the front. If you change to another window before that, the taskbar button will flash but the window will not be brought to the front.

Community
  • 1
  • 1
noachr
  • 91
  • 1
  • 8

2 Answers2

5
  • toFront(quite common issue) doesn't works for JFrame, this is basic property for JDialog

  • basically is possible to move toFront() only one JFrame, have to use setExtendedState, but with side effects flickering and jumping on the scren, use JDialog instead

  • don't use KeyListener, because JFrame isn't focusable for KeyEvent, have to use KeyBindings

for example

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class WindowTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();

    public WindowTest() {
        frame.setTitle("Window Test");
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setExtendedState(JFrame.ICONIFIED);
        Timer t = new Timer(3000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                java.awt.EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        frame.setExtendedState(JFrame.NORMAL);
                    }
                });
            }
        });
        t.setRepeats(false);
        t.start();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                final WindowTest wt = new WindowTest();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

There's another trick that can be used in case you don't want to minimize and then maximize the window. I have no idea why it works, but if you move the mouse before making the window visible it will come to the front. It's pretty weird, I know, but it seems to work for JRE 1.4 through 1.8 at least. In order to minimize the effect on the mouse you can first see where it is and only move it a little. Your code might look something like this:

PointerInfo mInfo = MouseInfo.getPointerInfo();
Point mWhere = mInfo.getLocation();
(new Robot()).mouseMove(mWhere.x + 2, mWhere.y + 2); 
frame_.setVisible(true);

I realize this is a rather late response for the person who posted the question but others may still be looking for the answer.

Garr
  • 11
  • 2