Introduction to JTabbedPane in Java
Java Swing package is part of the Java’s foundation classes, JFCs.These foundation classes are used to create graphical user interfaces, window-based applications using available GUI components which makes easier for a programmer to develop desktop applications. They are written completely in java top of AWT, Abstract Windows Toolkit.
But opposite to AWT, Java Swing packages provide platform-independent components. These components are comparatively light weighted too.
At the end of this article, you will be able to add Swing JComponents like JTabbedPane onto an AWT Container. You will be also able to set the position of your panel and frame. You also will learn how to create a scroll-able tab panel.
What is JTabbedPane?
JTabbedPane is one of the classes provided by the swing package in java. It is very useful, as it provides the flexibility to the user to switch between different groups of components he desires to see, by simply clicking on one of the tabs. The tabs can be given different titles or icons.
Here is an everyday example of such a window, that has several tabs that can be accessed by clicking on the tabs.
A simple ‘properties’ window of a local disk (here, Local Disk F: Properties window on my system) on your computer system has multiple tabs, named General, Tools, Hardware, etc that you are able to access, by clicking on one of them, is a perfect example of the tabbed panes.
In other words, the JTabbedPane helps you to have several components share the same place. The user easily chooses which component he wants to see by choosing or clicking on the desired tab.
JTabbedPane Tab Indexing and Placement
The tabs are represented by an index, which gets decided according to the place at or the position in which the tab was added.
To understand this, let’s suppose you added your first tab. Then its index will be equal to ‘0’ and your next tab will have an index equal to ‘1’ and going by the fashion, your last added tab’s index will be equal to ‘tab count minus 1’.
The Tabbed Pane classes use a single selection model that represents ‘a set of tab indexes’ as well as the ‘currently selected index’ i.e. the selected tab’s index.
By default, the tabs are placed at the TOP location, as you can see in the above window’s property tabbed pane. You can change this tab placement to any of the directions like LEFT, RIGHT or Bottom by the use of a method called, setTabPlacement method.
JTabbedPane Constructors in Java
While using JTabbedPane, we will be using its constructors too which are differentiated based on the privileges they provide.
The commonly used constructors while using JTabbedPane are listed as follows:
1. JTabbedPane( )
The constructor JTabbedPane( ) creates an empty TabbedPane. When we use the constructor JTabbedPane( ), it creates the pane without any specification for its placement. So the tab is placed on its default place that is TOP as discussed above using JTabbedPane.TOP.
2. JTabbedPane(int tabPlacement)
This constructor creates an empty TabbedPane. It provides the privilege to decide the direction or place for the pane. The placement is done when the direction is specified using JTabbedPane.TOP, JTabbedPane.BOTTOM, JTabbedPane.LEFT, or JTabbedPane.RIGHT.
3. JTabbedPane(int tabPlacement, int tabLayoutPolicy)
An empty tabbed pane is created when this constructor is used. This provides the privilege to decide the placement for the tab-pane the same as the JTabbedPane(int tabPlacement) constructor.
It also lets the programmer decide the tab layout policy. This allows the programmer to control how the tabs will be displayed. The tab layout policy is either set to JTabbedPane.SCROLL_TAB_LAYOUT or JTabbedPane.WRAP_TAB_LAYOUT. By default, the policy is settoWRAP_TAB_LAYOUT.
With the policy set to SCROLL_TAB_LAYOUT, the tabs become scroll-able and a button for scrolling the tabs, left-right or up-down, is displayed in your tabbed pane.
Here, we discuss Example of JTabbedPane in Java in details:
Finally some exercise, shall we begin?
This is the part where we will try and add the tabbed pane in our JFrame. We will use javax.swing.JTabbedPane package will provide us the feature to add tabs onto our frame.
Following is an example code, using JTabbedPane to create tabs in our frame.
public class JTP_Layout
//Creating our JFrame
frame= newJFrame("Tabbed Pane Sample");
//Terminating process after exiting
//Adding TabbedPane Object
JTabbedPanetabbedPane = new JTabbedPane();
//Adding TabbedPane LayoutPolicy
//Setting our componentobject
//JRadioButton r1=new JRadioButton("A) Tab Component A");
//Adding our First Panel Object
JPanel p1=new JPanel();
//Adding our component to the Panel
JPanel p2=new JPanel();
JPanel p3=new JPanel();
//Setting boundary values for the tabs
//Naming 'One' asour First Pane
//Setting Frame'sposition to the center
//Setting visibility asTrue
public static void main(String args)
When the above program runs, we get the following window as the output.
In the above example, we added tabs onto our JFrame and gave different names to our tabs as ‘One’, ‘Two’, etc. We can also add different components under our different tabs using a simple code line such as below.
We created an object for the radio button, r1 and then this object was used to add radio buttons to the panel One i.e. p1.
And the ‘Radio Button’ component will appear under our first panel, where we added it.
Using JTabbedPane Constructors
We already used Constructors such as JTabbedPane(int tabPlacement) or JTabbedPane(int tabPlacement, int tabLayoutPolicy) in our program but we still haven’t seen it’s an effect.
Understand this, we created the Java class for ourselves, and then called our constructor, JTP_Layout (see the code). Then we set arguments such as setTabLayoutPolicy to SCROLL_TAB_LAYOUT using our class’s object.
As mentioned above, when the tablayout policy is set to SCROLL, it allows the user to scroll tabs in a direction using a button.
For illustration I increased the number of tabs in our program and set the policy to SCROLL, see it’s the effect:
You can see a scroll button on the right corner, which allows the user to go left-right using this button.
The other such argument added to our constructor is setTabPlacement, which is as discussed above is set to TOP by default. But our constructor helps us decide this by setting the tab placement to BOTTOM or LEFT or RIGHT.
We already had such code offline in our program, but commented (see the code), when the comment is removed, see the effect.
Observe that when we used tabbedPane.setTabPlacement(JTabbedPane.BOTTOM), our tabs relocated to the bottom of the frame.
Here, important to note is, there is no need for any ‘Error Handling’, our JTabbedPane object handles your mouse and keyboard strokes or events. Still, if you want similar functionality without the tab interface, you can use a card layout instead of a tabbed pane.
In the end, if said in simple words, for creating our tabbed pane, instantiate JTabbedPane, then create components to be displayed and then add these components to your desired tab.
That’s it, it was easy right?
This is a guide to JTabbedPane in Java. Here we discuss the introduction to JTabbedPane and JTabbedPane constructors along with its example. You may also look at the following articles to learn more-