Java status bar
This might save someone else a little bother. I’ve been building Java Swing demo’s in the last few months and one thing that has slowed me down is building a status bar. A status bar is the strip at the bottom of a window that contains useful information about the status of an application. As an example, here is the status bar for my skype client:
There is no standard swing control (container) to do this so you must roll your own. Searching for “java status bar” didnt get me very far. Initially I just used a JLabel, but this is too simple. I really wanted something with multiple recessed panels (as in the skype example). I considered using an unfloatable JToolBar but I dropped this idea because a toolbar’s default background doesnt look like a status bar and because its not easy to make the recessed panels this way. Finally I realised that the best way to build my status bar is to nest JPanels. In the next screenshot you can see an example Java status bar which consists of the one JPanel with two JPanel containers nested within it.
Now it’s a little tricky to get the status bar behaving correctly on resize, but if you use a GridBagLayout for your top level JPanel then it’s doable. However, a GridBagLayout is pretty horrible to use so I constructed two helper classes, StatusBar and StatusBarPanel to make things eaiser. A StatusBarPanel is simply a JPanel extended with two extra attributes: an integer panelWidth and a boolean fixedWidth. Thus you can define the width that you wish for your panel and whether or not it can stretch on resize. The StatusBar class also extends JPanel and has an array of StatusBarPanel objects. A StatusBar object configures the GridBagConstraints for you when you give it the array of panels.
Category: desktop, java, software 5 comments »


September 22nd, 2008 at 10:39 am
Hi, I find your StatusBar very interesting. Can you give me it’s source code?
Thanks
September 22nd, 2008 at 9:09 pm
Well it’s not much, but I’ve uploaded it: StatusBar.zip. HTH
November 1st, 2008 at 4:34 pm
Thanks very much.
August 27th, 2009 at 12:00 pm
thank you very much, quite helpful
September 25th, 2009 at 9:57 pm
Yes, good stuff for us beginners. Thanks!