JClass Elements

PreviousNextIndex

13

Splash Screen

Features of JCSplashScreen  Classes and Interfaces  Methods and Constructors  Examples 


13.1 Features of JCSplashScreen

A splash screen is an image that appears while an application is loading. It serves both as an indication that the program is being loaded from disk and as a place to put notices, such as copyrights, version or release numbers, and the like.

JCSplashScreen does the following:


13.2 Classes and Interfaces

The stand-alone class com.klg.jclass.swing.JCSplashScreen subclasses from java.lang.Object, providing an independent mechanism for displaying an image in a window in the middle of the screen.

No interfaces are used in JCSplashScreen.


13.3 Methods and Constructors

Constructors

JCSplashScreen()

JCSplashScreen has two constructors for instantiating a splash screen, one taking a String that specifies the location of the image, and the other taking an Icon. It throws an illegalArgumentException if the image String or image icon is invalid.

Methods

setVisible()

A Boolean method that shows or hides the splash screen. Once the splash screen is hidden, the splash screen window will be disposed. This means the splash screen cannot become visible again.


13.4 Examples

If you compile and run the code for this example, which is given below, you'll see a message printed on the console informing you that the application has started. The image for the splash screen is loaded and is made visible, then the program enters a wait state until a sleep command times out. An application that takes a long time to load would exhibit similar behavior. The user knows that loading is in progress because the splash screen is visible. It contains whatever graphic information you think is appropriate.

The example uses a JCExitFrame to hold a button that controls the disposal of the splash screen. All that is required for its disposal is the command setVisible(false), but once it is given, the splash screen is gone for good. You would issue this command after receiving notification that the application is ready to run.

Figure 43 :  The visible elements in SplashScreenExample.

import com.klg.jclass.swing.*;
import com.klg.jclass.util.swing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.*;

public class SplashScreenExample extends JFrame implements ActionListener {
static JCSplashScreen ss;
static Icon image;
SplashScreenExample() {
URL url = getClass().getResource("/images/SplashScreen.gif");
// convert URL to Image icon
image = new ImageIcon(url);
System.out.println("\n\nLoading an image using URL\n\nURL: " + url);
System.out.println("This example simulates the loading");
System.out.println("of a large application using a sleep command.");
// initialize(image);
ss = new JCSplashScreen(image);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
ss.setVisible(false);
}
}

public static void main(String[] args){
SplashScreenExample sse = new SplashScreenExample();
String title = "Splash Screen Appears while Application Loads";
JCExitFrame frame;
frame = new JCExitFrame(title);
ss = new JCSplashScreen(image);
ss.setVisible(true);
try {
Thread.currentThread().sleep(10000);
} catch (Exception e) {}
Container cp = frame.getContentPane();
JButton btn = new JButton("Click Me and I'll Close the Splash Screen!");
btn.addActionListener(sse);
cp.add(btn);
frame.setSize(450, 100);
frame.setVisible(true);

frame.setExitOnClose(true); // Close the window so the application can exit.

}


}


PreviousNextIndex