1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 import java.awt.event.*;
  26 import javax.swing.*;
  27 import javax.swing.event.*;
  28 
  29 /*
  30  * @test
  31  * @bug 4361477
  32  * @summary JTabbedPane throws ArrayOutOfBoundsException
  33  * @author Oleg Mokhovikov
  34  * @run main bug4361477
  35  */
  36 public class bug4361477 {
  37 
  38     static JTabbedPane tabbedPane;
  39     volatile static boolean bStateChanged = false;
  40     volatile static Rectangle bounds;
  41 
  42     public static void main(String args[]) throws Exception {
  43 
  44         Robot robot = new Robot();
  45         robot.setAutoDelay(50);
  46 
  47         SwingUtilities.invokeAndWait(new Runnable() {
  48 
  49             @Override
  50             public void run() {
  51                 createAndShowUI();
  52             }
  53         });
  54 
  55         robot.waitForIdle();
  56 
  57         SwingUtilities.invokeAndWait(new Runnable() {
  58 
  59             @Override
  60             public void run() {
  61                 bounds = tabbedPane.getUI().getTabBounds(tabbedPane, 0);
  62             }
  63         });
  64 
  65         Point location = bounds.getLocation();
  66         SwingUtilities.convertPointToScreen(location, tabbedPane);
  67         robot.mouseMove(location.x + 1, location.y + 1);
  68         robot.mousePress(InputEvent.BUTTON1_MASK);
  69         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  70 
  71         if (!bStateChanged) {
  72             throw new RuntimeException("Tabbed pane state is not changed");
  73         }
  74     }
  75 
  76     static void createAndShowUI() {
  77 
  78         final JFrame frame = new JFrame();
  79         tabbedPane = new JTabbedPane();
  80         tabbedPane.add("Tab0", new JPanel());
  81         tabbedPane.add("Tab1", new JPanel());
  82         tabbedPane.add("Tab2", new JPanel());
  83         tabbedPane.setSelectedIndex(2);
  84         tabbedPane.addChangeListener(new ChangeListener() {
  85 
  86             public void stateChanged(final ChangeEvent pick) {
  87                 bStateChanged = true;
  88                 if (tabbedPane.getTabCount() == 3) {
  89                     tabbedPane.remove(2);
  90                 }
  91             }
  92         });
  93 
  94         frame.getContentPane().add(tabbedPane);
  95         frame.setSize(300, 200);
  96         frame.setVisible(true);
  97     }
  98 }