Java Swing: JWindow appears behind all other process windows, and will not disappear

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();
        }

    });

Answers


You might want to take a look at java.awt.SplashScreen. However, if your heart is set on your solution:

Looking at Window#toFront,

If this Window is visible, brings this Window to the front and may make it the focused Window.

Try making your JWindow visible before bringing it to the front.

I'm not sure why your JWindow isn't hiding, that part works for me.

/e1 If you're trying to implement a singleton pattern, you should make your constructor and field private. You might also want to take a look at Efficient way to implement singleton pattern in Java.

/e2 The returns at the end of your showSplash and hideSplash methods are unnecessary, the method would have returned at that point anyways.


Need Your Help

How to turn off or disable vfs file loading in JBOSS AS7

java jboss classloader vfs

I am using jboss AS7 as server and also i am using the following code for getting all jar files which contains particular resource file.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.