Number of rows the JTabbedPane is using to display its tabs

According to the javax.swing.JTabbedPane.getTabRunCount() API documentation:

Returns the number of tab runs currently used to display the tabs.

Returns:

an integer giving the number of rows if the tabPlacement is TOP or BOTTOM and the number of columns if tabPlacement is LEFT or RIGHT, or 0 if there is no UI set on this tabbedpane

This is basically a convenience method for javax.swing.plaf.TabbedPaneUI.getTabRunCount(), which has no API documentation.

The code for TabRunTest takes the following command line params:

  1. TOP (default) | BOTTOM (1) | LEFT (2) | RIGHT (3)

  • creates the specified number of tabs or 10 (if not specified or negative).

    There is a Get Info button at the bottom of the window to display the tab count and tab run count

    TabRowTest.java:

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
     
    /**
     *@author     Administrator
     *@created    February 4, 2002
     */
    public class TabRowTest extends JPanel {
     
       public static void main(String[] args) {
          JFrame frame = new JFrame("Multiple Colors...");
     
          java.awt.Container contentPane = frame.getContentPane();
          contentPane.setLayout(new BorderLayout());
     
          int tabPlacement = JTabbedPane.TOP;
          if (args.length > 0) {
             try {
                int temp = Integer.parseInt(args[0]);
                switch (temp) {
                   case 1:
                      tabPlacement = JTabbedPane.BOTTOM;
                      break;
                   case 2:
                      tabPlacement = JTabbedPane.LEFT;
                      break;
                   case 3:
                      tabPlacement = JTabbedPane.RIGHT;
                      break;
                }
             } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println();
                System.out.println("------------------CONTINUING--------------------");
                System.out.println();
             }
          }
     
          int numberOfTabs = 10;
          if (args.length > 1) {
             try {
                numberOfTabs = Integer.parseInt(args[1]);
                if (numberOfTabs < 0) {
                   numberOfTabs = 10;
                }
             } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println();
                System.out.println("------------------CONTINUING--------------------");
                System.out.println();
             }
          }
     
          contentPane.add(new TabRowTest(tabPlacement, numberOfTabs));
     
          frame.setSize(800, 600);
     
          frame.addWindowListener(
             new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent ev) {
                   System.exit(0);
                }
             });
     
          frame.show();
       }
       // main
     
       public TabRowTest(int tabPlacement, int numberOfTabs) {
          super(new BorderLayout());
     
          final JTabbedPane tabbedPane = new JTabbedPane(tabPlacement);
          add(tabbedPane, BorderLayout.CENTER);
     
          for (int idx = 1; idx < (numberOfTabs + 1); idx++) {
             tabbedPane.addTab("Tab #" + idx, new JLabel("Tab #" + idx + " stuff"));
          }
     
          JPanel infopanel = new JPanel(new GridLayout(1, 0));
          add(infopanel, BorderLayout.SOUTH);
     
          final JLabel tabCountLabel = new JLabel("<tab count>");
          final JLabel runCountLabel = new JLabel("<run count>");
     
          JButton button = new JButton("Get Info");
          infopanel.add(button);
          infopanel.add(new JLabel("# Tabs:"));
          infopanel.add(tabCountLabel);
          infopanel.add(new JLabel("Run Count:"));
          infopanel.add(runCountLabel);
     
          button.addActionListener(
             new ActionListener() {
                public void actionPerformed(ActionEvent ev) {
                   int tabCount = tabbedPane.getTabCount();
                   int runCount = tabbedPane.getTabRunCount();
                   tabCountLabel.setText("" + tabCount);
                   runCountLabel.setText("" + runCount);
                }
             });
       }
       // TabRowTest
    }