Search code examples
javaswingjtabbedpanejprogressbar

Java: JProgressBar (or equivalent) in a JTabbedPane tab title


If I really wanted to do something like that, can I put a JProgressBar (or it's equivalent) in a JTabbedPane tab? (I mean, not in the tab itself,

How would I do something like that?

EDIT I really do want to put the progressbar in the title of the tab, not the tab itself.

Here's some ascii art:

----------------------------------------------------
|  Tab 1  || Tab   2||Tab-with-progress-bar||Tab  4|
-----------         --------------------------------
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
----------------------------------------------------

So it's really "tab 2" that is currently visible, but I want the progress bar (or equivalent) to be visible in the third tab's title.

EDIT 2

This has to work on Java 1.5: this has to work on the countless MacOS 10.4 and MacOS 10.5 Apple computers that will never be equipped with Java 6 (some do, some don't: and quite a lot never will, it is not my call)


Solution

  • Enclose the JProgressbar in a JPanel and add that JPanel to the JTabbedPane.

    Edit: From the JTabbedPane JavaDoc:

    // In this case the custom component is responsible for rendering the title of the tab.

    tabbedPane.addTab(null, myComponent); 
    tabbedPane.setTabComponentAt(0, new JLabel("Tab"));
    

    So you could basically simply replace new JLabel("Tab") by a reference to your JProgressbar (though this JProgressbar must not be added to the Tab itself). However, I think this method doesn't exist prior to Java 1.6.