evotopid
evotopid

Reputation: 5429

Show SplashScreen Programmatically

I'm currently working on a Java Application and it's my first Java App. So I created a file Splash.png and placed it into the source folder resourcesof the application.

I already managed it to show the Splash image on startup, with the JVM param -splash:resources/Splash.png, but my question is;

How can I show this splash screen again, but programmatically?

I need this functionality for the About menu item.

Upvotes: 8

Views: 4249

Answers (4)

evotopid
evotopid

Reputation: 5429

Thanks for your great help. I thought there isn't really any function which does this, so I just coded a JFrame, which I can show instead of the Splash screen on launch. For the case that someone could need the code also I just post it here:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class AboutWindow extends JFrame implements MouseListener {

    public AboutWindow() {
        // show splash screen image
        ImageIcon icon = new ImageIcon("resources/Splash.png");
        JLabel label = new JLabel(icon);
        getContentPane().add(label, BorderLayout.CENTER);

        // setup window correct
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setUndecorated(true);

        pack();

        // place it at the right position
        Dimension windowDimension = Toolkit.getDefaultToolkit().getScreenSize();

        int x = (windowDimension.width-getWidth())/2;
        int y = (windowDimension.height-getHeight())/3;

        setLocation(x, y);

        // add the mouse listener
        addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        dispose();
    }

    @Override
    public void mousePressed(MouseEvent me) {
        // do nothing
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        // do nothing
    }

    @Override
    public void mouseEntered(MouseEvent me) {
        // do nothing
    }

    @Override
    public void mouseExited(MouseEvent me) {
        // do nothing
    }
}

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168825

How can I show this splash screen again, but programmatically?

The AWT based SplashScreen API that functionality uses, offers the getImageURL() method that might be handy in that respect. The SplashScreen instance needs to be obtained early in the main() before any GUI elements are visible on-screen.

When it comes time to display the image, there are a multitude of possibilities best suited to different tasks and ways of using the image. One of the easiest ways to display it would be:

JLabel splashLabel = new JLabel(new ImageIcon(splashURL));

Pop that in a dialog or window as mentioned by mKorbel to show it on-screen. Normally for 'splash-like' images we would want to go with pure AWT, but this situation is a bit different in that the GUI is already on-screen, so Swing would presumably be loaded and ready.

Upvotes: 1

GETah
GETah

Reputation: 21429

Here is an outstanding example on using splash screens programmatically

The -splash is also described there.

Upvotes: 4

AlexR
AlexR

Reputation: 115348

Use java.awt.SplashScreen class.

BTW I did not know that JVM has this cool options '-splash'. So, thanks for information!

Upvotes: 2

Related Questions