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:
package com.mycompany.guiexamples; import java.awt.Color; import java.awt.Font; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; /** * * @author Fawadkhan */ public class JTabbedPaneExample { public static void main ( String [ ] args ) { // creation of a new JDialog myJDialog = new JDialog ( ) ; myJDialog. setTitle ( "Testing JTabbedPane" ) ; myJDialog. setSize ( 450 , 300 ) ; // Here we create our JPanels JPanel Game = new JPanel ( ) ; JPanel Movies = new JPanel ( ) ; JPanel Downloads = new JPanel ( ) ; JPanel Tutorials= new JPanel ( ) ; JPanel Books = new JPanel ( ) ; JPanel Softwares = new JPanel ( ) ; JPanel Mobiles = new JPanel ( ) ; // showing label in tabs JLabel Gamelabel=new JLabel(); Gamelabel.setText("Game Tab"); Gamelabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Movieslabel=new JLabel(); Movieslabel.setText("Movies Tab"); Movieslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Downloadslabel=new JLabel(); Downloadslabel.setText("Downloads Tab"); Downloadslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Tutorialslabel=new JLabel(); Tutorialslabel.setText("Tutorials Tab"); Tutorialslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Bookslabel=new JLabel(); Bookslabel.setText("Books Tab"); Bookslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Softwareslabel=new JLabel(); Softwareslabel.setText("Softwares Tab"); Softwareslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Mobileslabel=new JLabel(); Mobileslabel.setText("Mobiles Tab"); Mobileslabel.setFont(new Font("Courier", Font.BOLD,50)); //adding Labels to tabs JPanel Game. add ( Gamelabel ) ; Movies. add ( Movieslabel ) ; Downloads. add ( Downloadslabel ) ; Tutorials. add ( Tutorialslabel ) ; Books. add ( Bookslabel ) ; Softwares. add ( Softwareslabel ) ; Mobiles. add ( Mobileslabel ) ; // Here we set the background colors for the JPanels Game.setBackground(Color.RED); Movies.setBackground(Color.BLUE); Downloads.setBackground(Color.GREEN); Tutorials.setBackground(Color.YELLOW); Books.setBackground(Color.PINK); Softwares.setBackground(Color.ORANGE); Mobiles.setBackground(Color.cyan); // Creation of a JTabbedPane object JTabbedPane tabpane = new JTabbedPane ( JTabbedPane . TOP , JTabbedPane.SCROLL_TAB_LAYOUT ) ; // This is where the JPanels are added as tabs tabpane. addTab ( "Games" , Game) ; tabpane. addTab ( "Movies" , Movies ) ; tabpane. addTab ( "Downloads" , Downloads ) ; tabpane. addTab ( "Tutorials" , Tutorials) ; tabpane. addTab ( "Books " , Books ) ; tabpane. addTab ("Softwares" , Softwares ) ; tabpane. addTab ("Mobiles" , Mobiles ) ; // JTabbedPane is added to our dialog myJDialog. add ( tabpane ) ; // We display our dialog myJDialog. setVisible ( true ) ; } }
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.
Example2: how to use wrap tab layout in java JTabbedPane:
Now Change the constant JTabbedPane.SCROLL_TAB_LAYOUT to JTabbedPane.WRAP_TAB_LAYOUT
package com.mycompany.guiexamples; import java.awt.Color; import java.awt.Font; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; /** * * @author Fawadkhan */ public class JTabbedPaneExample { public static void main ( String [ ] args ) { // creation of a new JDialog myJDialog = new JDialog ( ) ; myJDialog. setTitle ( "Testing JTabbedPane" ) ; myJDialog. setSize ( 450 , 300 ) ; // Here we create our JPanels JPanel Game = new JPanel ( ) ; JPanel Movies = new JPanel ( ) ; JPanel Downloads = new JPanel ( ) ; JPanel Tutorials= new JPanel ( ) ; JPanel Books = new JPanel ( ) ; JPanel Softwares = new JPanel ( ) ; JPanel Mobiles = new JPanel ( ) ; // showing label in tabs JLabel Gamelabel=new JLabel(); Gamelabel.setText("Game Tab"); Gamelabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Movieslabel=new JLabel(); Movieslabel.setText("Movies Tab"); Movieslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Downloadslabel=new JLabel(); Downloadslabel.setText("Downloads Tab"); Downloadslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Tutorialslabel=new JLabel(); Tutorialslabel.setText("Tutorials Tab"); Tutorialslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Bookslabel=new JLabel(); Bookslabel.setText("Books Tab"); Bookslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Softwareslabel=new JLabel(); Softwareslabel.setText("Softwares Tab"); Softwareslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Mobileslabel=new JLabel(); Mobileslabel.setText("Mobiles Tab"); Mobileslabel.setFont(new Font("Courier", Font.BOLD,50)); //adding Labels to tabs JPanle Game. add ( Gamelabel ) ; Movies. add ( Movieslabel ) ; Downloads. add ( Downloadslabel ) ; Tutorials. add ( Tutorialslabel ) ; Books. add ( Bookslabel ) ; Softwares. add ( Softwareslabel ) ; Mobiles. add ( Mobileslabel ) ; // Here we set the background colors for the JPanels Game.setBackground(Color.RED); Movies.setBackground(Color.BLUE); Downloads.setBackground(Color.GREEN); Tutorials.setBackground(Color.YELLOW); Books.setBackground(Color.PINK); Softwares.setBackground(Color.ORANGE); Mobiles.setBackground(Color.cyan); // Creation of a JTabbedPane object JTabbedPane tabpane = new JTabbedPane ( JTabbedPane . TOP , JTabbedPane.WRAP_TAB_LAYOUT ) ; // This is where the JPanels are added as tabs tabpane. addTab ( "Games" , Game) ; tabpane. addTab ( "Movies" , Movies ) ; tabpane. addTab ( "Downloads" , Downloads ) ; tabpane. addTab ( "Tutorials" , Tutorials) ; tabpane. addTab ( "Books " , Books ) ; tabpane. addTab ("Softwares" , Softwares ) ; tabpane. addTab ("Mobiles" , Mobiles ) ; // JTabbedPane is added to our dialog myJDialog. add ( tabpane ) ; // We display our dialog myJDialog. setVisible ( true ) ; } }
Example3: how to place tabs in left in java using JTabbedPane:
Now Change the constant JTabbedPane.TOP to JTabbedPane.LEFTÂ
package com.mycompany.guiexamples; import java.awt.Color; import java.awt.Font; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; /** * * @author Fawadkhan */ public class JTabbedPaneExample { public static void main ( String [ ] args ) { // creation of a new JDialog myJDialog = new JDialog ( ) ; myJDialog. setTitle ( "Testing JTabbedPane" ) ; myJDialog. setSize ( 450 , 300 ) ; // Here we create our JPanels JPanel Game = new JPanel ( ) ; JPanel Movies = new JPanel ( ) ; JPanel Downloads = new JPanel ( ) ; JPanel Tutorials= new JPanel ( ) ; JPanel Books = new JPanel ( ) ; JPanel Softwares = new JPanel ( ) ; JPanel Mobiles = new JPanel ( ) ; // showing label in tabs JLabel Gamelabel=new JLabel(); Gamelabel.setText("Game Tab"); Gamelabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Movieslabel=new JLabel(); Movieslabel.setText("Movies Tab"); Movieslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Downloadslabel=new JLabel(); Downloadslabel.setText("Downloads Tab"); Downloadslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Tutorialslabel=new JLabel(); Tutorialslabel.setText("Tutorials Tab"); Tutorialslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Bookslabel=new JLabel(); Bookslabel.setText("Books Tab"); Bookslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Softwareslabel=new JLabel(); Softwareslabel.setText("Softwares Tab"); Softwareslabel.setFont(new Font("Courier", Font.BOLD,50)); JLabel Mobileslabel=new JLabel(); Mobileslabel.setText("Mobiles Tab"); Mobileslabel.setFont(new Font("Courier", Font.BOLD,50)); //adding Labels to tabs JPanle Game. add ( Gamelabel ) ; Movies. add ( Movieslabel ) ; Downloads. add ( Downloadslabel ) ; Tutorials. add ( Tutorialslabel ) ; Books. add ( Bookslabel ) ; Softwares. add ( Softwareslabel ) ; Mobiles. add ( Mobileslabel ) ; // Here we set the background colors for the JPanels Game.setBackground(Color.RED); Movies.setBackground(Color.BLUE); Downloads.setBackground(Color.GREEN); Tutorials.setBackground(Color.YELLOW); Books.setBackground(Color.PINK); Softwares.setBackground(Color.ORANGE); Mobiles.setBackground(Color.cyan); // Creation of a JTabbedPane object JTabbedPane tabpane = new JTabbedPane ( JTabbedPane.LEFT , JTabbedPane.WRAP_TAB_LAYOUT ) ; // This is where the JPanels are added as tabs tabpane. addTab ( "Games" , Game) ; tabpane. addTab ( "Movies" , Movies ) ; tabpane. addTab ( "Downloads" , Downloads ) ; tabpane. addTab ( "Tutorials" , Tutorials) ; tabpane. addTab ( "Books " , Books ) ; tabpane. addTab ("Softwares" , Softwares ) ; tabpane. addTab ("Mobiles" , Mobiles ) ; // JTabbedPane is added to our dialog myJDialog. add ( tabpane ) ; // We display our dialog myJDialog. setVisible ( true ) ; } }
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. |