Path: blob/master/test/jdk/javax/swing/JList/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 JList.updateUI() for invokes updateUI() on its cellrenderer via29* SwingUtilities.updateComponentTreeUI().30* If the cellrenderer is a parent of this JList 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.DefaultListCellRenderer;40import javax.swing.JFrame;41import javax.swing.JList;42import javax.swing.JScrollPane;43import javax.swing.ListCellRenderer;44import javax.swing.SwingUtilities;4546public class UpdateUIRecursionTest extends JFrame implements ListCellRenderer {47JList list;48DefaultListCellRenderer renderer;4950public UpdateUIRecursionTest() {51super("UpdateUIRecursionTest");52setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);53setSize(400, 400);5455String[] listData = {56"First", "Second", "Third", "Fourth", "Fifth", "Sixth"57};5859list = new JList(listData);60list.setCellRenderer(this);61renderer = new DefaultListCellRenderer();62getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);6364setVisible(true);65}6667public static void main(String[] args) throws Exception {6869SwingUtilities.invokeAndWait(new Runnable() {7071@Override72public void run() {73UpdateUIRecursionTest obj = new UpdateUIRecursionTest();7475obj.test();7677obj.disposeUI();78}79});80}8182public void test() {83list.updateUI();84}8586public void disposeUI() {87setVisible(false);88dispose();89}9091public Component getListCellRendererComponent(JList list, Object value,92int index, boolean isSelected, boolean cellHasFocus)93{94return renderer.getListCellRendererComponent(list, value, index,95isSelected, cellHasFocus);96}97}9899100101