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 import sun.awt.SunToolkit;
  29 
  30 /*
  31  * @test
  32  * @key headful
  33  * @bug 4361477
  34  * @summary JTabbedPane throws ArrayOutOfBoundsException
  35  * @author Oleg Mokhovikov
  36  * @run main bug4361477
  37  */
  38 public class bug4361477 {
  39 
  40     static JTabbedPane tabbedPane;
  41     volatile static boolean bStateChanged = false;
  42     volatile static Rectangle bounds;
  43 
  44     public static void main(String args[]) throws Exception {
  45 
  46         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  47         Robot robot = new Robot();
  48         robot.setAutoDelay(50);
  49 
  50         SwingUtilities.invokeAndWait(new Runnable() {
  51 
  52             @Override
  53             public void run() {
  54                 createAndShowUI();
  55             }
  56         });
  57 
  58         toolkit.realSync();
  59 
  60         SwingUtilities.invokeAndWait(new Runnable() {
  61 
  62             @Override
  63             public void run() {
  64                 bounds = tabbedPane.getUI().getTabBounds(tabbedPane, 0);
  65             }
  66         });
  67 
  68         Point location = bounds.getLocation();
  69         SwingUtilities.convertPointToScreen(location, tabbedPane);
  70         robot.mouseMove(location.x + 1, location.y + 1);
  71         robot.mousePress(InputEvent.BUTTON1_MASK);
  72         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  73 
  74         if (!bStateChanged) {
  75             throw new RuntimeException("Tabbed pane state is not changed");
  76         }
  77     }
  78 
  79     static void createAndShowUI() {
  80 
  81         final JFrame frame = new JFrame();
  82         tabbedPane = new JTabbedPane();
  83         tabbedPane.add("Tab0", new JPanel());
  84         tabbedPane.add("Tab1", new JPanel());
  85         tabbedPane.add("Tab2", new JPanel());
  86         tabbedPane.setSelectedIndex(2);
  87         tabbedPane.addChangeListener(new ChangeListener() {
  88 
  89             public void stateChanged(final ChangeEvent pick) {
  90                 bStateChanged = true;
  91                 if (tabbedPane.getTabCount() == 3) {
  92                     tabbedPane.remove(2);
  93                 }
  94             }
  95         });
  96 
  97         frame.getContentPane().add(tabbedPane);
  98         frame.setSize(300, 200);
  99         frame.setVisible(true);
 100     }
 101 }