Path: blob/master/test/jdk/javax/swing/JComboBox/7195179/Test7195179.java
41153 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* @key headful38* @bug 719517939* @summary Tests that combobox works with generified renderers40* @author Sergey Malenkov41*/4243public class Test7195179 {44private static JFrame frame;45public static void main(String[] args) throws Exception {46invokeAndWait(new Runnable() {47@Override48public void run() {49try {50Integer[] items = {null, 1, 2, 3};51JComboBox<Integer> combo = new JComboBox<>(items);52JLabel label = new JLabel("choose:");53JPanel panel = new JPanel();54GroupLayout layout = new GroupLayout(panel);55panel.setLayout(layout);56label.setLabelFor(combo);57combo.setSelectedIndex(0);58combo.setRenderer(new ListCellRenderer<Integer>() {59private final BasicComboBoxRenderer renderer = new BasicComboBoxRenderer();6061@Override62public Component getListCellRendererComponent(JList<? extends Integer> list, Integer value, int index, boolean isSelected, boolean cellHasFocus) {63return this.renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);64}65});66layout.setAutoCreateContainerGaps(true);67layout.setAutoCreateGaps(true);68layout.setHorizontalGroup(layout.createSequentialGroup()69.addGroup(layout.createParallelGroup().addComponent(label))70.addGroup(layout.createParallelGroup().addComponent(combo)));71layout.setVerticalGroup(layout72.createSequentialGroup()73.addGroup(layout74.createParallelGroup(GroupLayout.Alignment.BASELINE)75.addComponent(label)76.addComponent(combo)));7778frame = new JFrame(getClass().getSimpleName());79frame.add(panel);80frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);81frame.pack();82frame.setVisible(true);83} finally {84if (frame != null) frame.dispose();85}86}87});88}89}909192