< prev index next >

test/javax/swing/JTabbedPane/7170310/bug7170310.java

Print this page


   1 /*
   2  * Copyright (c) 2014, 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.Component;
  25 import java.awt.Dimension;
  26 import java.awt.Rectangle;
  27 import java.awt.Toolkit;
  28 import javax.swing.JComponent;
  29 import javax.swing.JFrame;
  30 import javax.swing.JPanel;
  31 import javax.swing.JTabbedPane;
  32 import javax.swing.JViewport;
  33 import javax.swing.SwingUtilities;
  34 import javax.swing.UIManager;
  35 import javax.swing.plaf.metal.MetalLookAndFeel;
  36 
  37 import sun.awt.SunToolkit;
  38 
  39 /**
  40  * @test

  41  * @bug 7170310
  42  * @author Alexey Ivanov
  43  * @summary Selected tab should be scrolled into view.
  44  * @run main bug7170310
  45  */

  46 public class bug7170310 {
  47     private static final int TABS_NUMBER = 3;
  48 
  49     private static volatile JTabbedPane tabbedPane;
  50     private static volatile int count = 1;
  51 
  52     private static volatile JFrame frame;
  53 
  54     private static volatile Exception exception = null;
  55 
  56     public static void main(String[] args) throws Exception {
  57         try {
  58             UIManager.setLookAndFeel(new MetalLookAndFeel());
  59             SwingUtilities.invokeAndWait(bug7170310::createAndShowUI);
  60 
  61             SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  62             toolkit.realSync();
  63 
  64             for (int i = 0; i < TABS_NUMBER; i++) {
  65                 SwingUtilities.invokeAndWait(bug7170310::addTab);
  66                 toolkit.realSync();
  67             }
  68 
  69             SwingUtilities.invokeAndWait(bug7170310::check);
  70 
  71             if (exception != null) {
  72                 System.out.println("Test failed: " + exception.getMessage());
  73                 throw exception;
  74             } else {
  75                 System.out.printf("Test passed");
  76             }
  77         } finally {
  78             frame.dispose();
  79         }
  80     }
  81 
  82     private static void createAndShowUI() {
  83         frame = new JFrame("bug7170310");
  84         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85         frame.setSize(200, 100);
  86 
  87         tabbedPane = new JTabbedPane();
  88         tabbedPane.addTab("Main Tab", new JPanel());
  89 
  90         tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  91 
  92         frame.getContentPane().add(tabbedPane);
  93         frame.setVisible(true);
  94     }
  95 
  96     private static void addTab() {
  97         tabbedPane.addTab("Added Tab " + count++, new JPanel());
  98         tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);


   1 /*
   2  * Copyright (c) 2014, 2017, 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.Component;
  25 import java.awt.Dimension;
  26 import java.awt.Rectangle;
  27 import java.awt.Toolkit;
  28 import javax.swing.JComponent;
  29 import javax.swing.JFrame;
  30 import javax.swing.JPanel;
  31 import javax.swing.JTabbedPane;
  32 import javax.swing.JViewport;
  33 import javax.swing.SwingUtilities;
  34 import javax.swing.UIManager;
  35 import javax.swing.plaf.metal.MetalLookAndFeel;
  36 
  37 import sun.awt.SunToolkit;
  38 
  39 /**
  40  * @test
  41  * @key headful
  42  * @bug 7170310
  43  * @author Alexey Ivanov
  44  * @summary Selected tab should be scrolled into view.
  45  * @run main bug7170310
  46  */
  47 
  48 public class bug7170310 {
  49     private static final int TABS_NUMBER = 3;
  50 
  51     private static volatile JTabbedPane tabbedPane;
  52     private static volatile int count = 1;
  53 
  54     private static volatile JFrame frame;
  55 
  56     private static volatile Exception exception = null;
  57 
  58     public static void main(String[] args) throws Exception {
  59         try {
  60             UIManager.setLookAndFeel(new MetalLookAndFeel());
  61             SwingUtilities.invokeAndWait(bug7170310::createAndShowUI);
  62 
  63             SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  64             toolkit.realSync();
  65 
  66             for (int i = 0; i < TABS_NUMBER; i++) {
  67                 SwingUtilities.invokeAndWait(bug7170310::addTab);
  68                 toolkit.realSync();
  69             }
  70 
  71             SwingUtilities.invokeAndWait(bug7170310::check);
  72 
  73             if (exception != null) {
  74                 System.out.println("Test failed: " + exception.getMessage());
  75                 throw exception;
  76             } else {
  77                 System.out.printf("Test passed");
  78             }
  79         } finally {
  80             if (frame != null) { frame.dispose(); }
  81         }
  82     }
  83 
  84     private static void createAndShowUI() {
  85         frame = new JFrame("bug7170310");
  86         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  87         frame.setSize(200, 100);
  88 
  89         tabbedPane = new JTabbedPane();
  90         tabbedPane.addTab("Main Tab", new JPanel());
  91 
  92         tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  93 
  94         frame.getContentPane().add(tabbedPane);
  95         frame.setVisible(true);
  96     }
  97 
  98     private static void addTab() {
  99         tabbedPane.addTab("Added Tab " + count++, new JPanel());
 100         tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);


< prev index next >