BenCole
BenCole

Reputation: 2112

Visible JProgressBar while initializing main GUI

I'm building a fairly complicated GUI for a project I'm working on. Importantly, it contains (among other things) a JTabbedPane with 12+ panes of complex GUI components.

What I'm trying to do is display a JProgressBar when I'm instantiating these panes (creating and configuring; doing everything short of actually displaying). Actually, I'm hoping for an end result similar to the Eclipse splash screen:

enter image description here

Here is (updated to include SplashScreen) pseudo-code for what I'm trying to do:

ProgramManager:
private setupGUI() {
    mainGUI = new MainGUI(); // only instantiates internal JFrame field
    mainGUI.setup();
}

MainGUI:
public void setup() {
    //create and configure progress bar
    final SplashScreen ss = SplashScreen.getSplashScreen();
    JProgressBar jpb = new JProgressBar(){
        Graphics g = ss.createGraphics();
        @Override
        public void paint(Graphics pG) {
            // TODO Auto-generated method stub
            super.paint(g);
        }
    };
    jpb.setValue(0);        
    setup1stTab();
    //update Progress
    setup2ndTab();
    //update progress
    etc....
    ss.close();
}

Please let me know if this is just not possible, or if I'm simply going about it wrong. I've looked around and seen some mention of Threading/SwingWorker, but after messing around with that and the Observer/Observable stuff (admittedly only a little), I still can't figure it out.

Upvotes: 3

Views: 458

Answers (2)

Garrett Hall
Garrett Hall

Reputation: 30032

Assuming that setup() is being called in the EDT (Event Dispatch Thread), that code will work.

To invoke a method in the EDT do:

    SwingUtilities.invokeLater(new Thread() {
        @Override
        public void run() {
            setup();
        }
    };

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

To get something similar to the Eclipse splash, see java.awt.SplashScreen. Once the image is on-screen, it is possible to call SplashScreen.createGraphics() to get..

..a graphics context (as a Graphics2D object) for the splash screen overlay image, which allows you to draw over the splash screen.

Draw the progress bar on top of that.

Upvotes: 5

Related Questions