Path: blob/master/test/jdk/javax/swing/JTableHeader/6567433/UpdateUIRecursionTest.java
41153 views
/*1* Copyright (c) 2016, 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* @test25* @key headful26* @bug 656743327*28* @summary JTableHeader.updateUI() invokes updateUI() on its TableCellrenderer via29* SwingUtilities.updateComponentTreeUI().30* If the Tablecellrenderer is a parent of this JTableHeader, the method recurses31* endless.32* This test tests that the fix is effective in avoiding recursion.33*34* @run main/othervm UpdateUIRecursionTest35*/3637import java.awt.BorderLayout;38import java.awt.Component;39import javax.swing.JFrame;40import javax.swing.JLabel;41import javax.swing.JScrollPane;42import javax.swing.JTable;43import javax.swing.SwingUtilities;44import javax.swing.table.JTableHeader;45import javax.swing.table.TableCellRenderer;4647public class UpdateUIRecursionTest extends JFrame implements TableCellRenderer {48JTable table;4950public UpdateUIRecursionTest() {51super("UpdateUIRecursionTest");52setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);53setSize(400, 400);5455String[] columnNames = {56"First Name",57"Last Name",58"Sport",59"# of Years",60"Vegetarian"};6162Object[][] data = {63{"Mary", "Campione",64"Snowboarding", new Integer(5), new Boolean(false)},65{"Alison", "Huml",66"Rowing", new Integer(3), new Boolean(true)},67{"Kathy", "Walrath",68"Knitting", new Integer(2), new Boolean(false)},69{"Sharon", "Zakhour",70"Speed reading", new Integer(20), new Boolean(true)},71{"Philip", "Milne",72"Pool", new Integer(10), new Boolean(false)}73};7475JTableHeader tableHeader = new JTableHeader();76table = new JTable(data, columnNames);77table.setTableHeader(tableHeader);78tableHeader.setDefaultRenderer(this);7980getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);8182setVisible(true);83}8485public static void main(String[] args) throws Exception {8687SwingUtilities.invokeAndWait(new Runnable() {8889@Override90public void run() {91UpdateUIRecursionTest obj = new UpdateUIRecursionTest();9293obj.test();9495obj.disposeUI();96}97});98}99100public void test() {101SwingUtilities.updateComponentTreeUI(this);102}103104public void disposeUI() {105setVisible(false);106dispose();107}108109public Component getTableCellRendererComponent(JTable table, Object value,110boolean isSelected, boolean hasFocus, int row, int col) {111return new JLabel(String.valueOf(value));112}113}114115116117118