JTabbedPane in java with Examples using NetBeans

JTabbedPane in java

The javax.swing.JTabbedPane class is a container for tabs, also known as tabs. You’ve probably come across tabs in other applications. In more modern browsers, for example, you can display individual websites in a separate tab. For each tab, there is a button, the so-called tab, which can be used to quickly switch between the different tabs.

Each tab, in turn, is a component (typically JPanel) added to the JTabbedPane. 

First of all, let’s look at two constructors that exist in addition to the standard constructor, which we don’t want to mention explicitly here.


JTabbedPane Constructors:

Constructor description
JTabbedPane(int tabPlacement) This constructor creates a JTabbedPane object. An int value is passed as a parameter, which specifies the alignment of the tabs.
JTabbedPane(int tabPlacement, int tabLayoutPolicy) As above, but with a second parameter that specifies the arrangement of the tabs (e.g. all tabs in one line).

The following constants exist for the transfer parameter tabPlacement:

Constantly description
JTabbedPane.TOP The tab bar is located above the tab content
JTabbedPane.BOTTOM The tab bar is located below the tab content
JTabbedPane.LEFT The tab bar is to the left of the tab contents
JTabbedPane.RIGHT The tab bar is located to the right of the tab contents

There are only two constants for the tabLayoutPolicy transfer parameter:

Constantly description
JTabbedPane.WRAP_TAB_LAYOUT Here the tabs are moved to the next row if they no longer fit next to each other or below one another due to the size of the JTabbedPane.
JTabbedPane.SCROLL_TAB_LAYOUT As soon as the tabs no longer fit in a row, buttons for scrolling appear automatically so that the tabs that are not visible can also be reached.

Now let’s create a JTabbedPane with multiple tabs using different layouts in the following examples.



JTabbedPane Examples:

Example1: how to make scrollable tabs in java using JTabbedPane:

I start by creating seven JPanels in different colors.  Then I create a JTabbedPane object using the second of the constructors presented in the above table. Using the addTab method, I add the JPanel objects and a suitable title that will appear on the tab afterward. Finally, added the JTabbedPane to the dialog for display.

The tabs are listed next to each other due to the set layout SCROLL_TAB_LAYOUT. However, since the seventh tabs are not completely displayed, additional buttons appear in the form of arrows, which can be used to display the tabs that are not visible.

JTabbedPane


Example2: how to use wrap tab layout in java JTabbedPane:

Now Change the constant JTabbedPane.SCROLL_TAB_LAYOUT to JTabbedPane.WRAP_TAB_LAYOUT

JTabbedPane


Example3: how to place tabs in left in java using JTabbedPane:

Now Change the constant JTabbedPane.TOP to JTabbedPane.LEFT 

JTabbedPane


JTabbedPane Methods in java:

The JTabbedPane class has some special methods, of which we would like to list some important ones here:

Method short description
add or addTab These methods are available with different transfer parameters. Both ways serve the same purpose, which is to add a new tab.
void addChangeListener(ChangeListener l) With the addition of a ChangeListener, it can be detected when the user switches to another tab.
Component getSelectedComponent() This method gives you the currently selected tab component.
int getSelectedIndex() This method gets the index (position) of the selected tab.
Component getTabComponentAt(int index) This method returns the tab component that matches the passed index.
int getTabCount() Returns the number of tabs.
int indexOfComponent(Component component) Returns the index matching the passed tab component.
remove The remove method, like the add method, comes in different variants. Its purpose is to remove tabs.
void setSelectedComponent(Component c) This method is used to select the tab with the transferred component.
void setSelectedIndex(int index) This method is used to select the tab that matches the passed index.

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: