Java Swing: JWindow appears behind all other process windows, and will not disappear
Posted
by
Kim Jong Woo
on Stack Overflow
See other posts from Stack Overflow
or by Kim Jong Woo
Published on 2011-11-30T01:47:40Z
Indexed on
2011/11/30
1:50 UTC
Read the original article
Hit count: 270
I am using JWindow to display my splash screen during the application start up. however it will not appear in front of all windows as it should, and it will not disappear as well.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class MySplash {
public static MySplash INSTANCE;
private static JWindow jw;
public MySplash(){
createSplash();
}
private void createSplash() {
jw = new JWindow();
JPanel content = (JPanel) jw.getContentPane();
content.setBackground(Color.white);
// Set the window's bounds, centering the window
int width = 328;
int height = 131;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - width) / 2;
int y = (screen.height - height) / 2;
jw.setBounds(x, y, width, height);
// Build the splash screen
JLabel label = new JLabel(new ImageIcon("splash.jpg"));
JLabel copyrt = new JLabel("SplashScreen Test",
JLabel.CENTER);
copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
content.add(label, BorderLayout.CENTER);
content.add(copyrt, BorderLayout.SOUTH);
Color oraRed = new Color(156, 20, 20, 255);
content.setBorder(BorderFactory.createLineBorder(oraRed, 0));
}
public synchronized static MySplash getInstance(){
if(INSTANCE==null){
INSTANCE = new MySplash();
}
return INSTANCE;
}
public void showSplash(){
jw.setAlwaysOnTop(true);
jw.toFront();
jw.setVisible(true);
return;
}
public void hideSplash(){
jw.setAlwaysOnTop(false);
jw.toBack();
jw.setVisible(false);
return;
}
}
So in my main class which extends JFrame, I call my splash screen by
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
MySplash.getInstance().showSplash();
}
});
However, the JWindow appears behind the all open instances of windows on my computer. Hiding the JWindow also doesn't work.
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
MySplash.getInstance().hideSplash();
}
});
© Stack Overflow or respective owner