Tab panels in action
On the Java platform, you would use the JTabbedPane to create tab panels. However, unlike the tab in Firefox, which presents a button to close a particular tab (see figure), there is no intuitive way to close a tab created by JTabbedPane.
Tab in Firefox; note the button on the right to close the tab |
Tabs created by JTabbedPane |
Often you have to resort to creating a separate 'close tab' button somewhere in the window to achieve this.
In JavaSE 6, the next release of the JavaSE platform, you can now add a close button or any other Swing component to a tab by using setTabComponentAt() method. Using this method, you are basically responsible for rendering the title of a tab. Assuming we wish to create the following tab panel:
A tab panel with a standard JButton in each tab
Using this new feature, here is the code snippet to do this.
JTabbedPanetabPanel =
new JTabbedPane(); |
A RPG type game
To use the cliche, the possibilities are truly limitless. All the sources (and more) for the examples described in this article can be found in this zip file.
Lee Chuk-Munn has been programming in the Java language since 1996, when he first joined Sun Microsystems in Hong Kong. He currently works as a senior developer consultant and technology evangelist for Technology Outreach at Sun in Singapore. Chuk's focus is in Java APIs, Java EE, Java SE, and Java ME. Chuk graduated in 1987 from the Royal Melbourne Institute of Technology in Melbourne, Australia, where his favorite subject was compiler theory.