2

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

    });
KJW
  • 15,035
  • 47
  • 137
  • 243
  • Why use a home-baked solution when there is the JWS Splash for 1.2+ & [SplashScreen](http://docs.oracle.com/javase/7/docs/api/java/awt/SplashScreen.html) for 1.6+? Why use Swing in a splash (for speed they generally use AWT, like the J2SE `SplashScreen`)? Why not post an [SSCCE](http://sscce.org/) rather than code that cannot be run to see the effect? – Andrew Thompson Nov 30 '11 at 02:12
  • @Kim Jong Woo +1 nice example – mKorbel Nov 30 '11 at 06:25

1 Answers1

4

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 What is an efficient way to implement a 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.

Community
  • 1
  • 1
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • yeah I fixed it. it seems that I was running SwingUtilities.invokeLater on the JFrame gui creation. removing it now displays the splashscreen correctly. thank you. – KJW Nov 30 '11 at 02:41
  • You can also add the following to your manifest and it will display a splash screen with no other work... "SplashScreen-Image: images/splash.gif" Where images/splash.gif is a resource in the jar – BillRobertson42 Nov 30 '11 at 05:26
  • yeah but I need to display license information on the splash screen, by first checking if the key is valid on teh servers. – KJW Nov 30 '11 at 06:41
  • 1
    @KimJongWoo You could display a splash screen while checking if the key is valid, then use `setImageURL` and `update` to change the image. – Jeffrey Nov 30 '11 at 22:33