简体   繁体   中英

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:

在此输入图像描述

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM