I want a behavior similar to eg Firefox where the list of available tabs does only show up if at least two tabs exist.
I wasn't able to find anything like that, yet.
The best idea I had was changing the layout manually:
While this would probably work it feels like a hack or workaround...
Any better idea?
A solution should ideally work in both Java 1.5 and 1.6... but I'd be happy about a 1.6-only solution, too.
You can override the UI method that calculates the height for the tab button area, forcing the height to 0
when there's only one tab:
tabbed_pane.setUI(new BasicTabbedPaneUI() {
@Override
protected int calculateTabAreaHeight(int tab_placement, int run_count, int max_tab_height) {
if (tabbed_pane.getTabCount() > 1)
return super.calculateTabAreaHeight(tab_placement, run_count, max_tab_height);
else
return 0;
}
});
I believe you'll have to do it manually. Apparently it has been done before , but only as a small bit of a system which seems to not be available.
Your approach looks good to me. I would do it just like you laid it out, and wrap all that logic in a custom JComponent
so it will feel less hackish.
简单地使用CardLayout
可能会更好。
Yes, there is a way. Took me four hours to find at the oracle website: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setTabLayoutPolicy()
Simply use this:
//declare
private JTabbedPane editor = new JTabbedPane ();
//construct like this:
editor.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
//just add components and see how it goes.
editor.addTab("", newPanel);
Another option would be to customize the L&F delegate (either BasicTabbedPaneUI or WindowsTabbedPaneUI depending on the platforms you care about) used by the JTabbedPane. This would allow you to customize the behavior of the tabbed pane in the case where only a single tab was being shown.
This is another way of doing things however I would say it's quite an undertaking and doing what Michael said will get you where you want to go with a lot less effort. I just wanted to post this as an answer in case you weren't aware of this option.
I think this can be achieved using tab bar and a card layout,
and this should do the job.
jTabbedPane1.removeTabAt(0);
seems to work after initComponents();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.