0

I am trying to create a loading screen for my program so it will display the loading screen while loading the main screen or while executing a particular task. I tried to use a JDialog as the loading screen but for some reason, once the loading screen dialog opens up, it doesnt let the main program run its own job. How do I make them work both at the same time?

Here is the code for my loading screen:

public class FrmLoading extends JDialog {

  private static FrmLoading loading;

  public FrmLoading(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    this.setLocationRelativeTo(null);
  }

  public static void startAnimation(){
    loading = new frmLoading(null, true);
    loading.setVisible(true);
  }

  public static void stopAnimation(){
    loading.dispose();
  }

  private void initComponents(){
    //build the Dialog
  }
}

this is how I use it:

private void login(){
    FrmLoading.startAnimation();
    //open main program
}

...

private void mainStart(){
    //load contents
    FrmLoading.stopAnimation();
}

I know that there is a loadingscreen default to java swing but I dont want to use it, I want the animation I made.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
John
  • 836
  • 2
  • 25
  • 39
  • I woud guess letting `modal = false` would do the trick. Still, the default [SplashScreen](http://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html) can do much. – Joop Eggen Nov 27 '12 at 16:22
  • I already tried this but it did not display my loading screen – John Nov 27 '12 at 16:41

3 Answers3

3

Use SwingWorker

SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
    @Override
    protected Void doInBackground() {
        //do your stuff in background
        return null;
    }

    @Override
    protected void done() {
        frmMain.setVisible(true);
        loading.dispose();
        disposeThis(); //a method that calls this.dispose();
    }
};
worker.execute();
loading.setVisible(true);
eSuarez
  • 152
  • 10
  • where should I place this? on the FrmLoading class or the main class? – John Nov 27 '12 at 16:20
  • Use it in your superclass. When the worker is executed, you set your dialog to visible. In doInBackground method, place all the stuff you want to do. When it's completed, in done(), you just set it to not visible, or call dispose() for your loading dialog. – eSuarez Nov 27 '12 at 16:24
  • the problem is this, when my program starts it will go to the FrmLogin class. From there, once the user press the login button, it will start running the FrmLoading. The FrmLogin will open up FrmMain where in the FrmMain will load all its contents and once done, will then stop the FrmLoading. So in the snippet that you provided, how do I do it? thanks btw for the help. – John Nov 27 '12 at 16:28
  • Place the loading of your FrmMain in the same worker. As you are only using your FrmLoading for presentation, you can load everything from your FrmLogin while your FrmLoading is showing. When the loading is complete, open your main instance created and dispose both the login and loading instances. – eSuarez Nov 27 '12 at 16:41
2

Why to use JFrame for loading screen at all. Consider using something like a JWindow:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class LoadingScreenDemo extends JWindow{
    Panel panel = new Panel();


    public LoadingScreenDemo() {
        addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
                if(e.getClickCount() == 2){
                    dispose();
                }
            }
        });
        panel.setBackground(Color.YELLOW);
        add(panel);
    }


    public static void main(String[] args) {
        LoadingScreenDemo m = new LoadingScreenDemo();
        m.setSize(640,480);
        m.setLocationRelativeTo(null);
        m.setVisible(true);
    }

    class Panel extends JPanel{

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setFont(new Font("Verdana",Font.BOLD,24));
            g.drawString("Loading...", 270, 210);

            g.setFont(new Font("Verdana",Font.PLAIN,14));
            g.drawString("OS: "+System.getProperty("os.name"), 10,400);
            g.drawString("OS version"+System.getProperty("os.version"), 10,420);
            g.drawString("Java vendor: "+System.getProperty("java.vendor"), 10,440);
            g.drawString("Java version: "+System.getProperty("java.version"), 10,460);
        }
    }

}

enter image description here

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • do you simulating the [SplashScreen](http://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html) – mKorbel Nov 27 '12 at 17:26
1

Instead of using the boolean modal arguement in the JDialog constructor, you may want to use the JDialog constructor with modality, and pass in the MODELESS enum.

This may stop the main window from blocking.

Jose
  • 1,616
  • 4
  • 26
  • 38