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