Display all the titles of JTabbedPane tabs on Console in Java

Java 8Object Oriented ProgrammingProgramming

To display all the titles of the JTabbedPane, let us first get the count of tabs −

int count = tabbedPane.getTabCount();

Now, loop through the number of tabs in the JTabbedPane. Use the getTitleAt() to get the title of each and every tab −

for (int i = 0; i < count; i++) {
   String str = tabbedPane.getTitleAt(i);
   System.out.println(str);
}

The following is an example to display all the titles of JTabbedPane tabs on Console −

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("Laptop", panel1);
      tabbedPane.addTab("Desktop ", panel2);
      tabbedPane.addTab("Notebook", panel3);
      tabbedPane.addTab("Echo ", panel4);
      tabbedPane.addTab("Tablet", panel5);
      tabbedPane.addTab("Alexa ", panel6);
      tabbedPane.addTab("Notebook", panel7);
      tabbedPane.addTab("iPad", panel8);
      int count = tabbedPane.getTabCount();
      for (int i = 0; i < count; i++) {
         String str = tabbedPane.getTitleAt(i);
         System.out.println(str);
      }
      frame.add(tabbedPane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(600,350);
      frame.setVisible(true);
   }
}

On runnting, the console will display all the values of the tabs −

Output

raja
Published on 31-May-2019 11:03:13
Advertisements