1/*
2 * Copyright (c) 2013, 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
24import java.awt.Component;
25import javax.swing.GroupLayout;
26import javax.swing.JComboBox;
27import javax.swing.JFrame;
28import javax.swing.JLabel;
29import javax.swing.JList;
30import javax.swing.JPanel;
31import javax.swing.ListCellRenderer;
32import javax.swing.plaf.basic.BasicComboBoxRenderer;
33
34import static javax.swing.SwingUtilities.invokeAndWait;
35
36/*
37 * @test
38 * @key headful
39 * @bug 7195179
40 * @summary Tests that combobox works with generified renderers
41 * @author Sergey Malenkov
42 */
43
44public class Test7195179 {
45    public static void main(String[] args) throws Exception {
46        invokeAndWait(new Runnable() {
47            @Override
48            public void run() {
49                Integer[] items = {null, 1, 2, 3};
50                JComboBox<Integer> combo = new JComboBox<>(items);
51                JLabel label = new JLabel("choose:");
52                JPanel panel = new JPanel();
53                GroupLayout layout = new GroupLayout(panel);
54                panel.setLayout(layout);
55                label.setLabelFor(combo);
56                combo.setSelectedIndex(0);
57                combo.setRenderer(new ListCellRenderer<Integer>() {
58                    private final BasicComboBoxRenderer renderer = new BasicComboBoxRenderer();
59
60                    @Override
61                    public Component getListCellRendererComponent(JList<? extends Integer> list, Integer value, int index, boolean isSelected, boolean cellHasFocus) {
62                        return this.renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
63                    }
64                });
65                layout.setAutoCreateContainerGaps(true);
66                layout.setAutoCreateGaps(true);
67                layout.setHorizontalGroup(layout.createSequentialGroup()
68                        .addGroup(layout.createParallelGroup().addComponent(label))
69                        .addGroup(layout.createParallelGroup().addComponent(combo)));
70                layout.setVerticalGroup(layout
71                        .createSequentialGroup()
72                        .addGroup(layout
73                                .createParallelGroup(GroupLayout.Alignment.BASELINE)
74                                .addComponent(label)
75                                .addComponent(combo)));
76
77                JFrame frame = new JFrame(getClass().getSimpleName());
78                frame.add(panel);
79                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
80                frame.pack();
81                frame.setVisible(true);
82            }
83        });
84    }
85}
86