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

Adding Same component to JTabbedPane throws ArrayIndexOutOfBoundsException

XMLWordPrintable

    • beta
    • x86, sparc
    • linux, solaris_2.6, windows_nt

      If a user adds a component that already exists in JTabbedPane will throw a ArrayIndexOutOfBoundsException. The problems seems that we get an invalid index
      after deletion. We need to insert current index of tabbedpane not the pages.size().

      TestCode:

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

      public class AddSameComp extends JFrame implements ActionListener{
        String path;
        Container contentPane = getContentPane();
        JTabbedPane jtp = new JTabbedPane();
        JButton b1 = new JButton("button in panel 1");
        JButton b2 = new JButton("button in panel 2");
        JPanel panelOne = new JPanel();
        JPanel panelTwo = new JPanel();
        JPanel panelThree = new JPanel();
        JLabel newTab = new JLabel("This is a New Tab Added");
        String textOut;
        

        private JPanel buttonPanel = new JPanel();
        private JPanel bottomPanel = new JPanel(new BorderLayout());
        private JPanel statusPanel = new JPanel();
        Object o;
        
        private JLabel label = new JLabel("Press Blue \"Add Component one again\" button to start test.");
        private JButton start;
        private JButton pass;
        private JButton fail;

        AddSameComp() {
          super("AddSameComp Test");
          
          // Add Invoke Buttons
          
          start = addButton(buttonPanel,"Add Component one again",Color.blue);
          pass = addButton(buttonPanel,"Pass",Color.green);
          fail = addButton(buttonPanel,"Fail",Color.red);
          bottomPanel.add(buttonPanel,BorderLayout.NORTH);
          statusPanel.add(label);
          bottomPanel.add(statusPanel,BorderLayout.SOUTH);

          contentPane.add(bottomPanel,BorderLayout.SOUTH);

          
          // Add Tab Panes Here!!!
          jtp.add("Panel One",panelOne);
          jtp.add("Panel Two",panelTwo);

          contentPane.add(jtp,BorderLayout.CENTER);
       
          // Add Window handler
          addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent event){ System.exit(0);}
          });
        }

         public void actionPerformed(ActionEvent evt) {
          String cmd = evt.getActionCommand();
          if (evt.getSource() == pass) {
            System.out.println("Test Passed");
            System.exit(0);
          }
          else if (evt.getSource() == fail) {
            System.out.println("Test Failed");
            System.exit(1);
          }
          else if (evt.getSource() == start) {
            try {
      jtp.add("Panel One",panelOne);
      label.setText("T est passed! ArrayIndexOutOfBoundsException was not thrown!!");
            }
            catch (ArrayIndexOutOfBoundsException e) {
      label.setText("Test Failed! ArrayIndexOutOfBoundsException was thrown!!");
            }
          }
        }

        public JButton addButton(JPanel addComponent, String name, Color color) {
          JButton b = new JButton(name);
          b.setBackground(color);
          b.addActionListener(this);
          addComponent.add(b);
          return b;
        }

        public static void main(String[] args) {
          
          AddSameComp t = new AddSameComp();
          t.setSize(450,300);
          t.setVisible(true);
        }
      }

            amfowler Anne Fowler (Inactive)
            collins Gary Collins (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: