Solved

How to create a splash screen for my program?

Posted on 2003-11-28
14
383 Views
Last Modified: 2010-05-18
i have a simple program, and i would like to add splash screen to it to make it look "cool". But i really don't know where/how to start..

public class myprogram extends JFrame{

  public myprogram(){//code}

  public void static main(String[] args){
    myprogram a = new myprogram();
    a.show();
  }
}

where should i add the splashscreen? i'm thinking of creating another JFrame and show it first, and then myprogram, but i failed, the splash screen only appears for 0.000000001 second i guess..

please help
tks
0
Comment
Question by:JAVAnewbie
  • 4
  • 4
  • 4
  • +2
14 Comments
 

Expert Comment

by:pkasturi
ID: 9836309
Hi,

You can put a progress bar with a fixed time for completion. We have done that for about 3-5 seconds and it also looks as if it is loading some files.

Adding a Progress bar is pretty straight forward.

Prashanth

0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 25 total points
ID: 9836517
Create a JWindow and start off tghere in.
A complete course in writing a splash screen is found in:
http://www.javaworld.net/javaworld/javatips/jw-javatip104.html 
Success!

;JOOP!
0
 

Author Comment

by:JAVAnewbie
ID: 9836526
pkasturi>> can you show me some sample code? it will help me to better understading..
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JAVAnewbie
ID: 9836541
sciuriware>>i visited the link, and get the following code:

class SplashWindow1 extends JWindow
{
    public SplashWindow1(String filename, Frame f)
    {
        super(f);
        JLabel l = new JLabel(new ImageIcon(filename));
        getContentPane().add(l, BorderLayout.CENTER);
        pack();
        Dimension screenSize =
          Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        setLocation(screenSize.width/2 - (labelSize.width/2),
                    screenSize.height/2 - (labelSize.height/2));
        setVisible(true);
        screenSize = null;
        labelSize = null;
    }
}

but i don't understand what is the parameter Frame is use for? how can i add this piece of coding inside my program? please provide some sample code if possible.. tks
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9836542
Don't use a ProgressBar when there is nothing progressing, don't fool your customer.
Just set a time limit in the right code.
;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9836553
First create a JFrame, the mainframe of your program, that owns almost all visible things.
Then give that JFrame instance to the code you found.
;JOOP!
0
 

Author Comment

by:JAVAnewbie
ID: 9836723
is it sth like this?

public class myprogram extends JFrame{

  public myprogram(){//code}

  public void static main(String[] args){
    myprogram a = new myprogram();
    splashscreen sp = new splashscreen("pic.jpg", a);
    sp.show();
  }
}
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9836752
Yes, does it work?
;JOOP!
0
 
LVL 1

Expert Comment

by:FesterWim
ID: 9838255
maybe a bit better:

public void static main(String[] args){

  myprogram a = new myprogram();

  final SplashScreen splashScreen = new SplashScreen( "pic.jpg", a);
  SwingUtilities.invokeLater( new Runnable()
  {
    public void run()
    {
       splashScreen.setVisible( true );
    }
   } );

  a.setVisible(true);
}

Also class names should start with an uppercase letter, so it should be MyProgram and not myprogram.

0
 
LVL 15

Accepted Solution

by:
Javatm earned 25 total points
ID: 9840408
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Splash extends JWindow {

private int waitTime;

 public Splash()
  {

// Insert your picture here :

  JLabel l1 = new JLabel(new ImageIcon("Images/Splash1.jpg"));
  JLabel l2 = new JLabel(new ImageIcon("Images/Splash2.gif"));  

  getContentPane().add(l1, BorderLayout.CENTER);
  getContentPane().add(l2, BorderLayout.SOUTH);
  getContentPane().setBackground(Color.black);
  pack();

  Dimension sd = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

  setLocation(sd.width / 2 - 310 / 2,
  sd.height / 2 - 265 / 2);

  setSize(310, 265);
  setVisible(true);
   }

  public static void main( String args[] )
   {
   Splash app = new Splash();
   }  
}
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9840410
That will solve your problem :)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9840417
>> JLabel l1 = new JLabel(new ImageIcon("Images/Splash1.jpg"));
     
      Just dont forget to insert the image like :

      JLabel l1 = new JLabel(new ImageIcon("YourFolder/YourPicture.jpg"));
 
0
 

Author Comment

by:JAVAnewbie
ID: 9847359
sciuriware>> the last time i try i doesnt really work, wonder if its the problem of JLabel..

let me try again tonight.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9853633
Have you tried my other solution ?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question