Bringing the front swinging window forward
I have a swing program that creates a full-screen borderless window - I'm running on Windows 7. I need a program to be able to focus and bring myself to the fore. However, when I try to use the method found here How to bring window to front? , instead of bringing the window to the front, the window just blinks in the taskbar and doesn't accept input. Below I wrote a small program that demonstrates the problem:
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 window, the maximum window, and then after three seconds try to bring it to the front. If you switch to another window before then, the taskbar button will blink, but the window will not be brought to the front.
source to share
-
toFront(quite common issue)
does not work forJFrame
, this is the main property forJDialog
-
basically
toFront()
only one can be movedJFrame
, usesetExtendedState
, but if side effects flicker and jump on scren useJDialog
instead -
do not use KeyListener because it is
JFrame
not configurable forKeyEvent
, you need to use Keyboard Shortcuts
eg
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();
}
});
}
}
source to share
There's another trick there that you can use if you don't want to minimize and then maximize the window. I have no idea why this works, but if you move the mouse before making the window visible, it will come to the front. It's kind of weird, I know, but it seems to work for JRE 1.4 through 1.8. To minimize the impact on the mouse, you can first see where it is and move it slightly. 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 fairly late answer for the person who posted the question, but others may still find the answer.
source to share