3

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.

BenCole
  • 2,092
  • 3
  • 17
  • 26
  • Did you notice the part of the docs that mentions *"..hidden automatically when the **first** AWT/Swing window is made visible."*? *Quote* from docs, **emphasis** by me. The reason I mention that is that *"no dice"* provides no useful information & the code snippets above do not make it entirely clear if **so much as one** GUI element is visible on-screen. You might tell me whether that is the case, but a better way to explain is with an [SSCCE](http://pscode.org/sscce.html). Please also try it first from the run-time command line option, & confirm that you are doing so. – Andrew Thompson Aug 05 '11 at 18:57
  • I had missed that actually, thanks. I'll have to find some other way to do this, because my program has two small windows for the users to go through before loading the main screen. Also, thanks for helping! I'm new to SO, and trying to learn the ropes as fast as I can. – BenCole Aug 05 '11 at 19:02
  • OK that rules out `SplashScreen`. :( I'd recommend a search on 'splashscreen+Java' to find the best code for throwing up a custom splash. There is a lot of it lying around. But beware any code that uses Swing. That underlines the author of the code is an amateur at creating splash screens. The original code should be pure AWT (like Oracle's `SplashScreen` implementation). (But obviously you need to dip into Swing for the progress-bar.) – Andrew Thompson Aug 05 '11 at 19:10
  • Alright, I'll go searching. Thanks for the assistance! – BenCole Aug 05 '11 at 19:14

2 Answers2

5

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I couldn't get this to work... I put the manifest option in: `SplashScreen-Image: files/cpuilogoa.gif` And then overrode my ProgressBar's paint() method to paint to the SplashScreen's graphics...but no dice. Was that the correct way to go about this? – BenCole Aug 05 '11 at 18:38
  • @Ben Please edit that code etc. into your question. I might comment on it, but reading code as 'text' spins my brain. – Andrew Thompson Aug 05 '11 at 18:43
  • Also, as an aside, how do you get newlines in comments? – BenCole Aug 05 '11 at 18:43
  • They figure newlines are not appropriate in a 'quick' comment. ;) – Andrew Thompson Aug 05 '11 at 18:44
  • Bah, humbug. Making my formatting OCD go wild... Anyway, there's the new stuff. – BenCole Aug 05 '11 at 18:45
1

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();
        }
    };
Garrett Hall
  • 29,524
  • 10
  • 61
  • 76