splash screen
Hello All:
I've decided to add a splash screen to a GUI app that I'm building.
However I'd like it to be Swing related if possible as opposed to AWT.
Does anyone know where or rather how I can start this.
I'm looking for two extremes of splash screens: a millisecond -- (dare I say) screen and a more conventional welcoming splash screen.
Any pointers in the right direction would be greatly appreciated.
Here some code, that can be used whenever a time consuming process is started:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
/**
* creates a splashscreen that runs a time consuming process
* in a separate thread, so that the splashscreen itself can be visualized properly
*/
abstract class SplashScreen extends JWindow
{
/**
* Constructor for a SplashScreen, that is located at the middle
* of the screen
* Shows a centered image above a message text
*
* Example:
* SplashScreen splash = new SplashScreen(..., ..., ...)
* {
*public void timeConsuming()
*{
*//insert the time consuming code here
*}
* };
* splash.start();
*
* @param component Parent component, the SplashScreen is positioned relative to it
* @param text Text that is shown at the SplashScreen
* @param image Image that is shown at the SplashScreen
*/
public SplashScreen(Component component, String text, String image)
{
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
Border bd1 = BorderFactory.createBevelBorder(BevelBorder.RAISED);
Border bd2 = BorderFactory.createEtchedBorder();
Border bd3 = BorderFactory.createCompoundBorder(bd1, bd2);
((JPanel)contentPane).setBorder(bd3);
ImageIcon icon = new ImageIcon(getClass().getResource(image));
contentPane.add("Center", new JLabel(icon, JLabel.CENTER));
contentPane.add("South", new JLabel(text, JLabel.CENTER));
setContentPane(contentPane);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
pack();
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
setLocation(screenWidth / 2 - getSize().width / 2, screenHeight / 2 - getSize().height / 2);
}
/**
* create an object of SplashScreen and override this method to include
* the code that is needed to be splashscreened
*/
public abstract void timeConsuming();
/**
* sets the splashscreen visible, starts the time consuming code
* and than disposes the splashscreen
*/
public void start()
{
Thread t = new Thread()
{
public void run()
{
Runnable r = new Runnable()
{
public void run()
{
timeConsuming();
dispose();
}
};
setVisible(true);
SwingUtilities.invokeLater(r);
}
};
t.start();
}
}