Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 253
BenCole Avatar asked Mar 29 '26 16:03

BenCole


2 Answers

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.

like image 139
Andrew Thompson Avatar answered Apr 01 '26 07:04

Andrew Thompson


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();
        }
    };
like image 44
Garrett Hall Avatar answered Apr 01 '26 07:04

Garrett Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!