1/* 2 * Copyright (c) 2010, 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/*@test 25 @bug 6670274 26 @summary Incorrect tab titles for JTabbedPane if using HTML (BasicTabbedPanelUI problem) 27 @author Alexander Potochkin 28 @run main bug6670274 29*/ 30 31import javax.swing.*; 32import javax.swing.plaf.basic.BasicTabbedPaneUI; 33import javax.swing.text.View; 34 35public class bug6670274 { 36 37 private static void createGui() { 38 final JTabbedPane pane = new JTabbedPane(); 39 TestTabbedPaneUI ui = new TestTabbedPaneUI(); 40 pane.setUI(ui); 41 42 pane.add("one", new JPanel()); 43 pane.add("<html><i>Two</i></html>", new JPanel()); 44 pane.add("three", new JPanel()); 45 pane.setTitleAt(0, "<html><i>ONE</i></html>"); 46 check(ui, 0, 1); 47 48 pane.setTitleAt(1, "hello"); 49 check(ui, 0); 50 51 pane.setTitleAt(0, "<html>html</html>"); 52 pane.setTitleAt(2, "<html>html</html>"); 53 check(ui, 0, 2); 54 } 55 56 private static void check(TestTabbedPaneUI ui, int... indices) { 57 for(int i = 0; i < ui.getTabbedPane().getTabCount(); i++) { 58 System.out.print("Checking tab #" + i); 59 View view = ui.getTextViewForTab(i); 60 boolean found = false; 61 for (int j = 0; j < indices.length; j++) { 62 if (indices[j]== i) { 63 found = true; 64 break; 65 } 66 } 67 System.out.print("; view = " + view); 68 if (found) { 69 if (view == null) { 70 throw new RuntimeException("View is unexpectedly null"); 71 } 72 } else if (view != null) { 73 throw new RuntimeException("View is unexpectedly not null"); 74 } 75 System.out.println(" ok"); 76 } 77 System.out.println(""); 78 } 79 80 81 static class TestTabbedPaneUI extends BasicTabbedPaneUI { 82 public View getTextViewForTab(int tabIndex) { 83 return super.getTextViewForTab(tabIndex); 84 } 85 86 public JTabbedPane getTabbedPane() { 87 return tabPane; 88 } 89 } 90 91 public static void main(String[] args) throws Exception { 92 SwingUtilities.invokeAndWait(new Runnable() { 93 public void run() { 94 bug6670274.createGui(); 95 } 96 }); 97 } 98} 99