Toolbar Toggle Swing Example
Started playing with Swing this last week and am finding it challenging and fun.
While learning the API, I came across a rather interesting problem where I needed to toggle a tool bar from being displayed in a window via a pop-up menu (regardless of whether it is embedded in the main window or dragged out in its own dialog window). For example:
One Window (where the main window and tool bar are one)

Two Windows (where the main window and tool bar are separated)

In my case, I needed to turn the tool bar on and off via a pop-up menu for both cases shown mentioned above. Here is a screen shot of the pop-up menu:

Turns out, this proved to be a rather difficult problem especially when the tool bar was popped out from the main window. My solution came about by adding a listener to my JCheckBoxMenuItem object:
…which would then call my showToolBar method:
Here is the full source code in case you want to run the example yourself:
Simply download the zip file, extract the *.java file, compile, and execute. The only compilation error you should have is the package name when running locally.
I would be interested to know if the code I wrote (where I check for correct parentage before disposing the tool bar window) is sound or not. Perhaps there is a smarter way to tackle this problem? In my tests, it seems to work fine without any flaws.
No comments yet.
Leave a comment
You must be logged in to post a comment.
Search
Categories
- Adventures
(91)
- Announcements
(28)
- Business
(16)
- Electronics
(21)
- Epicurean
(9)
- Games
(3)
- Literature
(1)
- Mechanical
(4)
- Meetings
(14)
- Movies
(2)
- Music
(26)
- Photography
(1)
- Services
(24)
- Software
(102)