Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

Custom Made SplashScreen class unusual behaviour.

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?
Avatar of harris_c
harris_c
Flag of Philippines image

 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);



  }
Avatar of prain

ASKER

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.
Avatar of prain

ASKER

OK. I have solved the problem. I had to incorporate a timer instead of delaying by using Thread.sleep().
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial