?
Solved

Custom Made SplashScreen class unusual behaviour.

Posted on 2007-09-30
5
Medium Priority
?
439 Views
Last Modified: 2013-12-29
Hello,
I have a terrible problem....

I am really working on Java 1.5. So I cannot use the SplashScreen class given in java 1.6. Therefore I picked up a SplashScreen class written by some on the net and tailored it. I run the SpalshScreen INDEPENDENTLY. That works great.

Then I created a separate JFrame and at the close of the frame (at the click of the X button at the Top RHS corner), I I am triggering the WindowClosing event and sopposed to see the SpalshScreen pops up. But this is how it behaves ....

1. In Java 1.5 - Only a white background displays and after the time out it goes away.

2. In Java 1.6 nothing is displayed, but at the timeout the main window who invokes the Spash exits.

My question is....
WHY IS THAT THE INDEPENDENT SplashScreen works and when Attached it behaves weiredly.

Here is the code of SplashScreen. Just compile and run this. Then I have the small window I have created to trigger the SplashScreen. That also in here for you to test out.

import java.awt.*;
import javax.swing.*;

public class SplashScreen extends JWindow {
  private int duration;
  public SplashScreen(int d) {
    duration = d;
  }

  // A simple little method to show a title screen in the center
  // of the screen for the amount of time given in the constructor
  public void showSplash() {
    JPanel content = (JPanel)getContentPane();
    content.setBackground(Color.white);

    // Set the window's bounds, centering the window
    int width = 450;
    int height =115;
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screen.width-width)/2;
    int y = (screen.height-height)/2;
    setBounds(x,y,width,height);

    // Build the splash screen
    JLabel label1 = new JLabel("Shutting down.....", JLabel.CENTER);
   
    label1.setFont(new Font("Sans-Serif", Font.BOLD, 12));
    content.add(label1, BorderLayout.NORTH);
   
   
    JPanel messagePanel = new JPanel(new GridLayout(2, 1));
    messagePanel.setBackground(Color.yellow);
    messagePanel.setPreferredSize(new Dimension(100, 30));
    JLabel label2 = new JLabel ("Closing Connection at port xxxx...", JLabel.CENTER);
    label2.setFont(new Font("Sans-Serif", Font.BOLD, 12));
    messagePanel.add(label2, BorderLayout.SOUTH);
   
    JLabel label3 = new JLabel ("Closing Connection at port yyyy...", JLabel.CENTER);
    label3.setFont(new Font("Sans-Serif", Font.BOLD, 12));
    messagePanel.add(label3, BorderLayout.SOUTH);
   
    content.add(messagePanel, BorderLayout.CENTER);
   
    JLabel label4 = new JLabel ("xxxxxxxxxx Version 1.0", JLabel.CENTER);
   
    label4.setFont(new Font("Sans-Serif", Font.BOLD, 12));
    content.add(label4, BorderLayout.SOUTH);
   
    Color oraRed = new Color(156, 20, 20,  255);
    content.setBorder(BorderFactory.createLineBorder(oraRed, 10));

    // Display it
    setVisible(true);

    // Wait a little while, maybe while loading resources
    try { Thread.sleep(duration); } catch (Exception e) {}

    setVisible(false);
  }

  public void showSplashAndExit() {
    showSplash();
    System.exit(0);
  }

  public static void main(String[] args) {
    // Throw a nice little title page up on the screen first
    SplashScreen splash = new SplashScreen(5000);
    // Normally, we'd call splash.showSplash() and get on with the program.
    // But, since this is only a test...
    splash.showSplashAndExit();
  }
}


================================= HERE IS THE TRIGGER CLASS==========
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PopSplash extends JFrame implements ActionListener
 {
       JButton splashButton;
       
  public PopSplash()
  {
    FlowLayout layout = new FlowLayout();
    this.setLayout(layout);
    this.setTitle("Window 1");
    this.setSize(335, 150);
    this.setLocation(200, 100);
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    this.addWindowListener(windowClosingListener);

    splashButton = new JButton("Splash");
    splashButton.setPreferredSize(new Dimension(95,30));
    splashButton.addActionListener(this);
 
    this.add(splashButton);
     
  }
 
  public void actionPerformed(ActionEvent aEvent)
  {
   if(aEvent.getSource() == splashButton)
   {
   
   }
  }


 WindowListener windowClosingListener = new WindowAdapter()
   {
      // anonymous WindowAdapter class
    public void windowClosing(WindowEvent w)
    {
     SplashScreen splash = new SplashScreen(5000);
     splash.showSplashAndExit();
     
     } // end windowClosing
   };// end anonymous class

  public static void main(String[] args) {
    PopSplash frame1 = new PopSplash();
    frame1.setVisible(true);
  }
}


Can some one throw some light to this?
0
Comment
Question by:prain
  • 2
4 Comments
 
LVL 8

Expert Comment

by:harris_c
ID: 19988952
 public void showSplashAndExit() {
    showSplash();
    System.exit(0);     //<- remove
  }

  public static void main(String[] args) {

     SplashScreen splash = new SplashScreen(5000);
     splash.showSplashAndExit();

    PopSplash frame1 = new PopSplash();
    frame1.setVisible(true);



  }
0
 

Author Comment

by:prain
ID: 19990160
Well I am not using this Splash as a start up. I am using this as a End Spash that will show the end of the program after the user decides to close the program. In the SpashScreen I am showing up some network disconnecting messages etc. To end the program, user will either select the "Exit" menut item or will click the Std. 'X' window closing icon at teh top RHS of the window. Then I am suposed to see this splash. That's why I have put that in the windowClosing() in my given example.
0
 

Author Comment

by:prain
ID: 19994395
OK. I have solved the problem. I had to incorporate a timer instead of delaying by using Thread.sleep().
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 20439019
PAQed with points refunded (125)

Computer101
EE Admin
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

809 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