Java Swing - GUIs
TOPIC 03 – MORE ON COMPONENTS

 

LESSON NOTE – JTABBEDPANE

 

 

This information about JTabbedPane comes from an older lesson that I created and therefore may have different formatting than other lessons.

 

 

INTRODUCING TABS

 

Tabs are a very common component used by GUIs to organize and give access to information.  They are used by operating systems, applications and web pages.  Here’s a simple example:

 

JTABBEDPANE CLASS

 

In Java, we can create tabs inside GUIs by using the JTabbedPane.java class.  This class extends the JComponent class and therefore can be added to a JFrame or JPanel.

 

Below is a simple example of a JTabbedPane:

 

 

HOW IT WORKS

 

Creating a basic tab system is fairly simple in Java.  You simply create a JTabbedPane object, add tabs to it and add it to the JFrame or JPanel.

 

ADDING TABS

 

Tabs require four pieces of information:

·        Tab text

·        Tab icon

·        Component to be displayed under tab (usually a JPanel)

·        Tool tip when mouse is hovering over tab

Note that if you want no image icon, you can simply place a null value in for that parameter.

 

EXAMPLE

 

We add a tab by using the following line:

 

          tabPaneObjName.addTab("Tab text", null, panelName, "Tool tip info");

          where null can be replaced by an icon.

 

SIZING

 

Note that the size of the entire tabbed pane object will depend on the size of the largest component that will be part of the tabs.

 

For our example, since we will be using empty JPanels, we will use the setPreferredSize(…) method to give the JPanel a non-empty size.

 

EXAMPLE CODE

 

Here’s a full example with a JTabbedPane created.  It includes no image icons.  For simplicity, the JPanels are each given a different background colour.  Remember that one of the JPanels needs to be given a preferred size in order to ensure a Tabbed Pane object of decent size – the example does this for JPanel #3 called pan3.

 

public class TabbedPanesFrame extends JFrame

{

      public TabbedPanesFrame()

      {

            Container cp = this.getContentPane();

            cp.setLayout(new FlowLayout());

           

            //Creating JTabbedPane here

           

            JTabbedPane tabbedPane = new JTabbedPane();

            ImageIcon icon = null;

 

            JPanel pan1 = new JPanel();

            pan1.setBackground(Color.BLACK);

            tabbedPane.addTab("Tab 1", icon, pan1, "Black Panel");

 

            JPanel pan2 = new JPanel();

            pan2.setBackground(Color.RED);           

            tabbedPane.addTab("Tab 2", icon, pan2, "Red Panel");

 

            JPanel pan3 = new JPanel();

            pan3.setBackground(Color.BLUE);          

            pan3.setPreferredSize(new Dimension(300,300));

            tabbedPane.addTab("Tab 3", icon, pan3, "Blue Panel");

 

            cp.add(tabbedPane);

            this.setSize(400,400);

            this.setVisible(true);

      }

     

      public static void main(String[] args)

      {

            TabbedPanesFrame tpjf = new TabbedPanesFrame();

      }

}

 

The code above will create the following:

 

 

TAB COUNT

 

We can get the number of tabs in the tabbed pane by using the following method call:

 

int total = tabPaneObjName.getTabCount()

 

REMOVING TABS

 

We can remove tabs based on their index number by using:

 

tabPaneObjName.remove(indexToRemove);

 

EXAMPLE

 

Here is an example that will include a button to add tabs and a button to remove tabs.  The code that specifies the preferred size of the panels exists on the panel of tab #3.  Using the button, remove tab #3 and notice what happens.

 

public class TabbedPanesFrame extends JFrame implements ActionListener

{

      JTabbedPane tabbedPane;

      JButton remButton;

      JButton addButton;

     

      public TabbedPanesFrame()

      {

            Container cp = this.getContentPane();

            cp.setLayout(new FlowLayout());

           

        //Creating JTabbedPane here

           

            tabbedPane = new JTabbedPane();

            ImageIcon icon = null;

 

        JPanel pan1 = new JPanel();

        pan1.setBackground(Color.BLACK);

        addButton = new JButton("Add Tab");

        addButton.addActionListener(this);

        pan1.add(addButton);

            tabbedPane.addTab("Adder", icon, pan1, "Adder");

 

        JPanel pan2 = new JPanel();

        pan2.setBackground(Color.RED);

 

        remButton = new JButton("Remove Tab");

        remButton.addActionListener(this);

        pan2.add(remButton);

            tabbedPane.addTab("Remover", icon, pan2, "Red Panel");

 

        JPanel pan3 = new JPanel();

        pan3.setBackground(Color.BLUE);        

        pan3.setPreferredSize(new Dimension(300,300));

        tabbedPane.addTab("Other", icon, pan3, "Blue Panel");

 

        cp.add(tabbedPane);

        this.setSize(400,400);

            this.setVisible(true);

      }

     

      public void actionPerformed(ActionEvent e)

      {

            if(e.getSource()==addButton)

            {

                  tabbedPane.addTab("New", null, new JPanel(), "New Panel");   

            }

            else

            {

                  int last = tabbedPane.getTabCount() - 1;

                  tabbedPane.remove(last);

            }    

      }

     

      public static void main(String[] args)

      {

            TabbedPanesFrame tpjf = new TabbedPanesFrame();

      }

}

The code above will create the following: