Search |
||
TabComponents in actionPosted by alexfromsun on November 9, 2005 at 10:51 AM PST
It's been a lot of publications about adding "close" button to a tabbedPane, all solutions suggested are really inventive and not easy to find. But we are gonna talk about Mustang "tabComponents feature" because it is the most preferable and clear solution for JTabbedPane customizing. Since 1.6 three new methods added to JTabbedPane: we can put a component to a tab with help of get this component with and test if a component is a tabComponent for this JTabbedPane with With this three methods we can enrich a JTabbedPane with comboboxes, multiple labels or whatever we want. Please download source files and go on. First, let's check how we can add the most wanted “close†buttons to a JTabbedPane TabComponent with a “close†button(source files in the blog.test1 package) I sublcassed JPanel and added there JLabel to show tab's title and a customized button which can close this tab – and that's it ! Here how it looks:
Notice that we use a real JButton here so we can set a tooltip, attach an ActionListener and customise it in any ways The only new thing is the close button's actionListener:
public void actionPerformed(ActionEvent e) {
int i = pane.indexOfTabComponent(ButtonTabComponent.this);
if (i != -1) {
pane.remove(i);
}
}
When we click to this button we find the index of the tab this button belongs to and close the tab. The only possible problem is that this tabComponent doesn't respect actual JTabbedPane's titles so if we remove all tabComponents
Notice that actual tab's titles looks like tab1 not like tabComponent1. In the next example we'll easily create better tabComponent which can show the actual titles. JRadioButton as a tabComponent(source files in the blog.test2 package) Let's say we don't like JTabbedPane's focus rendering and want to replace it with something else.
With this code: JRadioButton button = new JRadioButton() {
//This way we always have up-to-date tab's title
public String getText() {
int i = pane.indexOfTabComponent(this);
if (i != -1) {
return pane.getTitleAt(i);
}
return null;
}
};
our radioButton will alway show the actual tab's title. We only need to add one listener to radioButtons to select a tab when we select a JRadioButton Crazy tabComponent with two icons(source files in the blog.test3 package) It is possible to show not only a title from a tab, but an icon as well.
if we remove all tabComponents we'll see that actual title and actual icon are used:
We find the acual icon just like we find titles for the previous example ConclusionTabComponents make it possible to customise JTabbedPane component quite easily, let me give you some recommendations
Thanks »
Related Topics >>
Swing Comments
Comments are listed in date ascending order (oldest first)
|
||
|