Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/7195179/Test7195179.java
38918 views
/*1* Copyright (c) 2013, 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*/2223import java.awt.Component;24import javax.swing.GroupLayout;25import javax.swing.JComboBox;26import javax.swing.JFrame;27import javax.swing.JLabel;28import javax.swing.JList;29import javax.swing.JPanel;30import javax.swing.ListCellRenderer;31import javax.swing.plaf.basic.BasicComboBoxRenderer;3233import static javax.swing.SwingUtilities.invokeAndWait;3435/*36* @test37* @bug 719517938* @summary Tests that combobox works with generified renderers39* @author Sergey Malenkov40*/4142public class Test7195179 {43public static void main(String[] args) throws Exception {44invokeAndWait(new Runnable() {45@Override46public void run() {47Integer[] items = {null, 1, 2, 3};48JComboBox<Integer> combo = new JComboBox<>(items);49JLabel label = new JLabel("choose:");50JPanel panel = new JPanel();51GroupLayout layout = new GroupLayout(panel);52panel.setLayout(layout);53label.setLabelFor(combo);54combo.setSelectedIndex(0);55combo.setRenderer(new ListCellRenderer<Integer>() {56private final BasicComboBoxRenderer renderer = new BasicComboBoxRenderer();5758@Override59public Component getListCellRendererComponent(JList<? extends Integer> list, Integer value, int index, boolean isSelected, boolean cellHasFocus) {60return this.renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);61}62});63layout.setAutoCreateContainerGaps(true);64layout.setAutoCreateGaps(true);65layout.setHorizontalGroup(layout.createSequentialGroup()66.addGroup(layout.createParallelGroup().addComponent(label))67.addGroup(layout.createParallelGroup().addComponent(combo)));68layout.setVerticalGroup(layout69.createSequentialGroup()70.addGroup(layout71.createParallelGroup(GroupLayout.Alignment.BASELINE)72.addComponent(label)73.addComponent(combo)));7475JFrame frame = new JFrame(getClass().getSimpleName());76frame.add(panel);77frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);78frame.pack();79frame.setVisible(true);80}81});82}83}848586