Path: blob/master/test/jdk/javax/swing/JTabbedPane/4310381/bug4310381.java
41153 views
/*1* Copyright (c) 2012, 2017, 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/*24* Portions Copyright (c) 2012 IBM Corporation25*/2627/*28* @test29* @bug 4310381 8075918 800402930* @summary Text in multi-row/col JTabbedPane tabs can be truncated/clipped31@run main/manual bug431038132*/33import java.awt.GridBagConstraints;34import java.awt.GridBagLayout;35import java.awt.event.ActionEvent;36import java.util.concurrent.CountDownLatch;37import java.util.concurrent.TimeUnit;38import javax.swing.BorderFactory;39import javax.swing.JButton;40import javax.swing.JFrame;41import javax.swing.JPanel;42import javax.swing.JTextArea;43import javax.swing.SwingUtilities;44import javax.swing.UIManager;45import java.awt.Insets;46import javax.swing.UIManager;47import javax.swing.UnsupportedLookAndFeelException;48import java.util.concurrent.TimeUnit;49import java.util.concurrent.CountDownLatch;50import javax.swing.JTabbedPane;51import javax.swing.JLabel;52import java.awt.Dimension;5354public class bug4310381 {55private static TestUI test = null;56private static JFrame frame = null;57private static JTabbedPane tab = null;58private static JPanel panel = null;59private static CountDownLatch testLatch = null;6061private static void init() {62frame = new JFrame();63tab = new JTabbedPane();64panel = new JPanel();65createContentPane();66tab.setTabPlacement(JTabbedPane.TOP);67tab.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);68frame.setMinimumSize(new Dimension(100, 200));69}7071public static void main(String[] args) throws Exception {72testLatch = new CountDownLatch(1);73test = new TestUI(testLatch);7475// initialize the test UI system76SwingUtilities.invokeAndWait(() -> {77try {78test.createUI();79} catch (Exception ex) {80throw new RuntimeException("Exception while creating UI");81}82});8384// update the laf85for(UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {86// set laf and create the frame and its sub components87setLookAndFeel(laf);8889// update and show UI90SwingUtilities.invokeAndWait(() -> {91init();92frame.setTitle(laf.getClassName());93showUI();94});9596final CountDownLatch latch = new CountDownLatch(1);97if(!latch.await(10, TimeUnit.SECONDS)) {98frame.setVisible(false);99}100101// disposing the frame102SwingUtilities.invokeAndWait(() -> {103frame.dispose();104});105}106107boolean status = testLatch.await(1, TimeUnit.MINUTES);108if (!status) {109System.out.println("Test timed out.");110}111112if (test.testResult == false) {113disposeUI();114throw new RuntimeException("Test Failed.");115}116}117118public static void disposeUI() throws Exception {119SwingUtilities.invokeAndWait(() -> {120if(test != null) {121test.disposeUI();122}123});124}125126static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) throws Exception {127try {128UIManager.setLookAndFeel(laf.getClassName());129} catch (ClassNotFoundException | InstantiationException |130UnsupportedLookAndFeelException | IllegalAccessException e) {131disposeUI();132throw new RuntimeException(e);133}134}135136public static void showUI() {137frame.getContentPane().add(tab);138frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);139frame.setLocationRelativeTo(null);140frame.setPreferredSize(new Dimension(100, 200));141frame.pack();142frame.setVisible(true);143}144145private static void createContentPane() {146String a2z = "abcdefghijklmnopqrstuvwxyz";147148tab.addTab("0" + a2z + a2z, new JLabel((UIManager.getLookAndFeel())149.getName() + " Look and Feel"));150tab.addTab("1" + a2z, new JLabel("1" + a2z));151tab.addTab("2" + a2z, new JLabel("2" + a2z));152tab.addTab("3", new JPanel()); // The last tab in Metal isn't truncated, that's ok153}154}155156class TestUI {157158private static JFrame mainFrame;159private static JPanel mainControlPanel;160161private static JTextArea instructionTextArea;162163private static JPanel resultButtonPanel;164private static JButton passButton;165private static JButton failButton;166167private GridBagConstraints gbc;168private static GridBagLayout layout;169private final CountDownLatch latch;170public boolean testResult = false;171172public TestUI(CountDownLatch latch) {173this.latch = latch;174}175176public final void createUI() throws Exception {177mainFrame = new JFrame();178179layout = new GridBagLayout();180mainControlPanel = new JPanel(layout);181resultButtonPanel = new JPanel(layout);182183gbc = new GridBagConstraints();184185// Create Test instructions186String instructions187= "See for different look and feel tabbed panes titles \n"188+ " contain three dots(...) at the end if the pane size \n"189+ " cannot accommadate the title completely.\n"190+ "If yes, click on 'pass' else click on 'fail'\n";191192instructionTextArea = new JTextArea();193instructionTextArea.setText(instructions);194instructionTextArea.setEditable(false);195instructionTextArea.setBorder(BorderFactory.196createTitledBorder("Test Instructions"));197198gbc.gridx = 0;199gbc.gridy = 0;200mainControlPanel.add(instructionTextArea, gbc);201202// Add customization to this test ui203customize();204205// Create resultButtonPanel with Pass, Fail buttons206passButton = new JButton("Pass");207passButton.setActionCommand("Pass");208passButton.addActionListener((ActionEvent e) -> {209System.out.println("Pass Button pressed!");210testResult = true;211latch.countDown();212disposeUI();213});214215failButton = new JButton("Fail");216failButton.setActionCommand("Fail");217failButton.addActionListener((ActionEvent e) -> {218System.out.println("Fail Button pressed!");219testResult = false;220latch.countDown();221disposeUI();222});223224gbc.gridx = 0;225gbc.gridy = 0;226resultButtonPanel.add(passButton, gbc);227228gbc.gridx = 1;229gbc.gridy = 0;230resultButtonPanel.add(failButton, gbc);231232gbc.gridx = 0;233gbc.gridy = 2;234mainControlPanel.add(resultButtonPanel, gbc);235236mainFrame.add(mainControlPanel);237mainFrame.pack();238mainFrame.setVisible(true);239}240241public void disposeUI() {242mainFrame.dispose();243}244245private void customize() throws Exception {246// Customize the test UI title247mainFrame.setTitle("Tabbed Pane Title Test");248}249}250251