Insert a tab after the first tab of a JTabbedPane container

Java 8Object Oriented ProgrammingProgramming

We are inserting a tab just after the first tab by using the index value 1. Here, index 1 would be location 2nd i.e. just after the first tab of the JTabbedPane container.

The following is an example to insert a tab after the first tab −

Example

package my;
import javax.swing.*;
import java.awt.*;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Devices");
      JTabbedPane tabbedPane = new JTabbedPane();
      JTextArea text = new JTextArea(100,100);
      JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;
      panel1 = new JPanel();
      panel2 = new JPanel();
      panel2.add(text);
      panel3 = new JPanel();
      panel4 = new JPanel();
      panel5 = new JPanel();
      panel6 = new JPanel();
      panel7 = new JPanel();
      panel8 = new JPanel();
      tabbedPane.setBackground(Color.blue);
      tabbedPane.setForeground(Color.white);
      tabbedPane.addTab("Tablet", panel1);
      tabbedPane.addTab("Alexa ", panel2);
      tabbedPane.addTab("Laptop", panel3);
      tabbedPane.addTab("Desktop ", panel4);
      tabbedPane.addTab("Notebook", panel5);
      tabbedPane.addTab("Echo ", panel6);
      tabbedPane.addTab("Notebook", panel7);
      tabbedPane.addTab("iPad", panel8);
      JLabel label = new JLabel("Kindle");
      tabbedPane.insertTab("Kindle", new ImageIcon("icon.png"), label, "Kindle", 1);
      frame.add(tabbedPane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(600,270);
      frame.setVisible(true);
   }
}

The output is as follows. Here, we can see our new tab “Kindle”, just after the first tab −

Output

raja
Published on 31-May-2019 11:32:28
Advertisements