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:

skype status bar

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.

My java status bar

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.

5 thoughts on “Java status bar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s