Magazine
 

Tips & Tricks

 
1. Splash Screen in Java 6.0

Splash screens are standard part of many GUI applications to let the user know about starting of the application. AWT/Swing can be used to create splash screens in Java. Prior to Java SE 6, you need to create a window and include an image in it when main method starts to get the behavior of splash screen. But to get the output, Java run time needs to be fully initialized before the window appeared. A new feature in Java SE 6 allows an application to show a splash screen even before the Java runtime starts with the help of a new command-line option that enables the image to be displayed more quickly to the user i.e. even before starting of Java runtime. If you are using command line to run the application, generate splash screen using –splash command line switch followed by a colon,which is followed by the image name. For example, to display button.png image file as splash screen when running SplashScreenExample.java file, you can type the following in command line.

java -splash:button.png
SplashScreenExample

The splash screen image can be of GIF, PNG, or JPEG format and support transparency, translucency, and animation. When the application creates its first window the splash screen disappears.

Example Code:

import javax.swing.*;
import java.awt.*;
public class SplashScreenExample {
public static void main(String args[]) {
Runnable r = new Runnable() {
public void run() {
try {
Thread.sleep(1500);
}
catch (InterruptedException e) {
System.out.println(e.getMessage());
}
JFrame frame = new JFrame(“Splash Screen
Example!”);
frame.setDefaultCloseOperation

 

(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel( “Welcome to
JavaJazzUp”, JLabel.CENTER);
label.setBackground(Color.RED);
frame.add(label, BorderLayout.CENTER);
frame.setSize(200, 100);
frame.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}

Compile
javac SplashScreenExample.java

Run:

java -splash:button.png
SplashScreenExample

The imagecan be seen in the figure

above. It disappears before frame gets visible. The frame can be seen in the figure below.

2. Creating Tabs with swing

Many applications need to keep group of components and switch between them. For this tab componets are useful in which we can group components and switch between them clicking those tabs. For this, JtabbedPane class in

Nov 2007 | Java Jazz Up | 57
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69   Download PDF