Thursday, 2 February 2017

How to create a Splash Screen in Java Swing.

Splash screen is window, which is displayed during an application startup. Many programs display such a screen, possibly containing copyright information, resource loading status, etc. Splash screen can not be moved or closed normally. It can only be closed or moved programmatically. Most seen splash screen by java user is Netbeans splash screen.

It is very easy to create a splash screen in java using JWindow.

Here is the complete example.

import javax.swing.*;
import java.awt.*;
public class Splash {
 public static void main(String[] args) {

 // Throw a nice little title page up on the screen first

 showSplash(20000);
 System.exit(0); // replace with application code!
 }
 // A simple little method to show a title screen in the
 // center of the screen for a given amount of time.

 public static void showSplash(int duration) {
JWindow splash = new JWindow();
 JPanel content = (JPanel)splash.getContentPane();

 // set the window's bounds, centering the window

 int width = 240;
 int height = 120;
 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
 int x = (screen.width-width)/2;
 int y = (screen.height-height)/2;
 splash.setBounds(x,y,width,height);

 // build the splash screen

 JLabel label = new JLabel(new ImageIcon("picture069.jpg"));
 JLabel copyrt = new JLabel
 ("Copyright 1998, PussinToast Inc.", JLabel.CENTER);
 copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
 content.add(label, BorderLayout.CENTER);
 content.add(copyrt, BorderLayout.SOUTH);
 content.setBorder(BorderFactory.createLineBorder(Color.red, 10));

 // display it

 splash.setVisible(true);

 // Wait a little while, maybe while loading resources

 try { Thread.sleep(duration); } catch (Exception e) {}
 splash.setVisible(false);
 }
}

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...