test
test

Reputation: 18198

Java Splash screen

I'm trying to make to make some text appear before my applet loads so I've made a simple SSCCE(.org):

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

    public class test extends JApplet {
      public void init() {

                this.add(new JLabel("Button 1"));
                System.out.println("Hello world...");


                try {
                Thread.sleep(3000);
                }catch(Exception hapa) { hapa.printStackTrace(); }


      }
    }

If you run it, the Button 1 will appear AFTER the 3 seconds when it's suppose to appear BEFORE that... what am I doing wrong?

Upvotes: 0

Views: 2039

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

JustinKSU covered the technical part of the question.

A better strategy would be to use the image param to show a 'splash' before the applet appears. See Special Attributes of Applets for further details.

I want one for a fixed amount of time... not just the loading.

In that case, put a CardLayout in the applet. Add the 'splash' to the first card, the rest of the GUI to another. At the end of the init() create a non-repeating Swing Timer that will flip to the card with the main GUI.

E.G.

// <applet code='SplashApplet' width='400' height='400'></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SplashApplet extends JApplet {

    public void init() {
        final CardLayout cardLayout = new CardLayout();
        final JPanel gui = new JPanel(cardLayout);

        JPanel splash = new JPanel();
        splash.setBackground(Color.RED);
        gui.add(splash, "splash");

        JPanel mainGui = new JPanel();
        mainGui.setBackground(Color.GREEN);
        gui.add(mainGui, "main");

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                cardLayout.show(gui, "main");
            }
        };

        Timer timer = new Timer(3000, listener);
        // only needs to be done once
        timer.setRepeats(false);
        setContentPane(gui);
        validate();
        timer.start();
    }
}

Upvotes: 1

JustinKSU
JustinKSU

Reputation: 4989

I think the init() method has to return before the items are rendered.

Upvotes: 2

Related Questions