silverhawk
silverhawk

Reputation: 609

How to change tabs location in JTabbedPane?

I want to change tabs location from left to center. How can I do this? I think I must change Look&Feel, but I don't know how.

From this:

enter image description here

To this:

enter image description here

Upvotes: 0

Views: 507

Answers (2)

trashgod
trashgod

Reputation: 205785

The placement of tabs is determined by the JTabbedPane UI delegate, typically based on BasicTabbedPaneUI. Not every Look & Feel implementation supports centered tabs, so there's no property that will work by default across platforms.

As a concrete example, com.apple.laf.AquaLookAndFeel supports centered tabs, as shown below. The class com.apple.laf.AquaTabbedPaneUI, which implements the effect, is shown here.

Because the implementation is non-trivial, a better choice is to support the user's Look & Feel choice using Preferences. A suitable Look & Feel selection control is shown here and here.

image

The source for the example above is seen here.

Upvotes: 1

Ben
Ben

Reputation: 3518

As you already pointed out, you have to use an LookAndFeel which supports this design (centered Tab-Button).
When your selected LaF does not support this, you have to write your own TabbedPaneUI.
(But this may not be very easy.)

If you do not want to create your own TabbedPaneUI, you have to look for an existing custom TabbedPaneUI or TabbedPane-Component, which support this kind of layout.

You can take a look at this article, to get started:
http://www.javaworld.com/article/2072927/swing-gui-programmingloseandmaxtabbedpane--an-enha/swing-gui-programming/closeandmaxtabbedpane--an-enhanced-jtabbedpane.html

Upvotes: 4

Related Questions