Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4265662

ArrayIndexOutOfBoundsException when using JTabbedPane.setComponentAt

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs



      Name: skT88420 Date: 08/25/99


      To speed up our GUI we do not build the screens corresponding
      to a tab in JTabbedPane until the Tab is visible. To accomplish
      this, we add a label as a place holder for the complex screen
      and when this label's paint() method is called, we do a
      SwingUtilities.invokeLater() which replaces the label with the
      complex screen by calling setComponentAt().

      To demonstrate this, compile the TabBug.java program below and
      run it. When the screen is visible, click on the "Complex Tab"
      tab. An exception will be thrown and the "Complex Tab" tab will
      disappear! This worked fine in 1.2.2.

      Here is the exception thrown:

      Exception occurred during event dispatching:
      java.lang.ArrayIndexOutOfBoundsException: -1 < 0
              at java.util.Vector.elementAt(Vector.java:419)
              at javax.swing.JTabbedPane.getTitleAt(JTabbedPane.java:720)
              at javax.swing.plaf.basic.BasicTabbedPaneUI$ContainerHandler.componentAdded(BasicTabbedPaneUI.java:1877)
              at java.awt.Container.processContainerEvent(Container.java:1271)
              at java.awt.Container.processEvent(Container.java:1250)
              at java.awt.Component.dispatchEventImpl(Component.java:2529)
              at java.awt.Container.dispatchEventImpl(Container.java:1302)
              at java.awt.Component.dispatchEvent(Component.java:2443)
              at java.awt.Container.addImpl(Container.java:391)
              at javax.swing.JTabbedPane.setComponentAt(JTabbedPane.java:1003)
              at TabBug$2.run(TabBug.java:24)
              at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:156)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:300)
              at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:105)
              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:95)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:86)

      Here is a short class that demonstrates this bug:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class TabBug extends JTabbedPane
      {
          public TabBug()
              {
                      JLabel l = new JLabel("First Tab");
                      l.setHorizontalAlignment(JLabel.CENTER);
                      addTab("First Tab", null, l, "A Tooltip");

                      // We add a label which is a placeholder for a complex screen
                      // The label will be replaced with the complex screen after the
                      // paint() method on the label is called
                      l = new JLabel("Initializing screen...") {
                              public void paint(Graphics g)
                              {
                                      final JLabel tmpLabel = this;
                                      SwingUtilities.invokeLater(new Runnable() {
                                              public void run() {
                                                      int tabIndex = indexOfComponent(tmpLabel);
                                                      if (tabIndex >= 0)
                                                              setComponentAt(tabIndex, buildComponent());
                                              }
                                      });
                                      super.paint(g);
                              }
                      };
                      l.setHorizontalAlignment(JLabel.CENTER);
                      addTab("Complex Tab", null, l, "Another Tooltip");
          }

              private JComponent buildComponent()
              {
                      return new JLabel("Complex screen");
              }

          public static void main(String args[])
              {
                      TabBug panel = new TabBug();
                      
                      JFrame frame = new JFrame("TabBug");
                      frame.addWindowListener(new WindowAdapter() {
                              public void windowClosing(WindowEvent e) {System.exit(0);}
                      });
                      frame.getContentPane().add("Center", panel);
                      frame.pack();
                      frame.setVisible(true);
              }
      }
      (Review ID: 94373)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: