Path: blob/master/test/jdk/javax/swing/JTabbedPane/TabProb.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 821539625@key headful26@summary Verifies JTabbedPane preferred size calculation is wrong27for SCROLL_TAB_LAYOUT.28@run main TabProb29*/3031import java.awt.Component;32import java.awt.Container;33import java.awt.Color;34import java.awt.BorderLayout;35import java.awt.Dimension;36import java.awt.LayoutManager;37import java.awt.Insets;38import java.awt.Robot;39import javax.swing.BorderFactory;40import javax.swing.JFrame;41import javax.swing.JLabel;42import javax.swing.JPanel;43import javax.swing.JTabbedPane;44import javax.swing.SwingUtilities;45import javax.swing.UIManager;4647public class TabProb extends JFrame {48static TabProb tb1;49static TabProb tb2;50class FixLayout implements LayoutManager {51@Override52public void layoutContainer(Container C) {53Insets in = C.getInsets();54int w = 200 - in.left - in.right;55int h = 100 - in.top - in.bottom;56C.getComponents()[0].setBounds(in.top, in.left, w, h);57}58@Override59public Dimension minimumLayoutSize(Container C) {60return new Dimension(200, 100);61}62@Override63public Dimension preferredLayoutSize(Container C) {64return new Dimension(200, 100);65}66@Override67public void removeLayoutComponent(Component c) {68}69@Override70public void addLayoutComponent(String s, Component c) {71}72}7374public TabProb(int layoutPolicy) {75JTabbedPane tabpanel = new JTabbedPane();76tabpanel.setTabPlacement(JTabbedPane.TOP);77tabpanel.setTabLayoutPolicy(layoutPolicy);78JPanel panel = new JPanel(new FixLayout());79JLabel label = new JLabel("Label");80label.setBorder(BorderFactory.createLineBorder(Color.green, 3));81panel.add(label);82tabpanel.add("TEST", panel);83add(tabpanel, BorderLayout.CENTER);84setUndecorated(true);85setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);86}8788public static void main(String[] args) throws Exception {89for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {90System.out.println("Test for LookAndFeel " + laf.getClassName());91UIManager.setLookAndFeel(laf.getClassName());92test();93System.out.println("Test passed for LookAndFeel " + laf.getClassName());94}95}9697public static void test() throws Exception {98Robot robot = new Robot();99SwingUtilities.invokeAndWait(new Runnable() {100@Override101public void run() {102tb1 = new TabProb(JTabbedPane.SCROLL_TAB_LAYOUT);103tb1.pack();104tb1.setVisible(true);105106tb2 = new TabProb(JTabbedPane.WRAP_TAB_LAYOUT);107tb2.pack();108tb2.setVisible(true);109}110});111double tb1height = tb1.getPreferredSize().getHeight();112double tb2height = tb2.getPreferredSize().getHeight();113System.out.println(tb1height);114System.out.println(tb2height);115116robot.waitForIdle();117robot.delay(2000);118119SwingUtilities.invokeAndWait(() -> tb1.dispose());120SwingUtilities.invokeAndWait(() -> tb2.dispose());121122if (tb1height != tb2height) {123throw new RuntimeException("JTabbedPane preferred size calculation is wrong for SCROLL_TAB_LAYOUT");124}125}126}127128129