How to add a Tab in JTabbedPane with Java?

Java 8Object Oriented ProgrammingProgramming

Let us first create a JTabbedPane −

JTabbedPane tabbedPane = new JTabbedPane();

Create some panels −

JPanel panel1, panel2, panel3, panel4, panel5;
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();

Now, add tabs using the addTab() method and set the panels as well in which the tabs would be visible −

tabbedPane.addTab("PHP", panel1);
tabbedPane.addTab("Blockchain ", panel2);
tabbedPane.addTab("Matlab", panel3);
tabbedPane.addTab("JSP ", panel4);
tabbedPane.addTab("Servlet", panel5);

The following is an example to add a tab in JTabbedPane −

package my;
import javax.swing.*;
import java.awt.*;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Technologies");
      JTabbedPane tabbedPane = new JTabbedPane();
      JPanel panel1, panel2, panel3, panel4, panel5;
      panel1 = new JPanel();
      panel2 = new JPanel();
      panel3 = new JPanel();
      panel4 = new JPanel();
      panel5 = new JPanel();
      tabbedPane.addTab("PHP", panel1);
      tabbedPane.addTab("Blockchain ", panel2);
      tabbedPane.addTab("Matlab", panel3);
      tabbedPane.addTab("JSP ", panel4);
      tabbedPane.addTab("Servlet", panel5);
      frame.add(tabbedPane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(550,350);
      frame.setVisible(true);
   }
}

Output


raja
Published on 31-May-2019 12:33:52
Advertisements