1 /*
   2  * Copyright (c) 2006, 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* @test
  27    @bug 6406264
  28    @summary Tests that JComboBox's focusable popup can be shown.
  29    @author Mikhail Lapshin
  30    @run main bug6406264
  31  */
  32 
  33 import javax.swing.JComboBox;
  34 import javax.swing.JFrame;
  35 import javax.swing.SwingUtilities;
  36 import javax.swing.plaf.basic.BasicComboBoxUI;
  37 import javax.swing.plaf.basic.ComboPopup;
  38 import javax.swing.plaf.basic.BasicComboPopup;
  39 import java.awt.*;
  40 
  41 public class bug6406264 extends JComboBox {
  42 
  43     static JFrame frame;
  44     static bug6406264 comboBox;
  45 
  46     public static void main(String[] args) throws Exception {
  47 
  48         Robot robot = new Robot();
  49         // Setup and show frame
  50         SwingUtilities.invokeAndWait(
  51                 new Runnable() {
  52                     public void run() {
  53                         frame = new JFrame("JComboBox6406264 test");
  54                         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55 
  56                         comboBox = new bug6406264("One", "Two", "Three");
  57                         frame.getContentPane().add(comboBox);
  58 
  59                         frame.setLocationRelativeTo(null);
  60                         frame.pack();
  61                         frame.setVisible(true);
  62                     }
  63                 }
  64         );
  65 
  66         robot.waitForIdle();
  67 
  68         // Show popup
  69         SwingUtilities.invokeAndWait(
  70                 new Runnable() {
  71                     public void run() {
  72                         comboBox.showPopup();
  73                     }
  74                 });
  75         robot.waitForIdle();
  76 
  77         // Check that popup is visible
  78         SwingUtilities.invokeAndWait(
  79                 new Runnable() {
  80                     public void run() {
  81                         if (comboBox.getUI().isPopupVisible(comboBox) == false)
  82                         {
  83                             throw new RuntimeException("A focusable popup is not visible " +
  84                                     "in JComboBox!");
  85                         }
  86                     }
  87                 }
  88         );
  89 
  90         frame.dispose();
  91     }
  92 
  93     public bug6406264(Object ... items) {
  94         super(items);
  95     }
  96 
  97     public void updateUI() {
  98         setUI(new CustomComboBoxUI());
  99     }
 100 
 101     // Use FocusablePopup for our JComboBox
 102     private class CustomComboBoxUI extends BasicComboBoxUI {
 103         protected ComboPopup createPopup() {
 104             return new FocusablePopup(bug6406264.this);
 105         }
 106     }
 107 
 108     // Popup window which can take a focus
 109     private class FocusablePopup extends BasicComboPopup {
 110         public FocusablePopup(JComboBox combo) {
 111             super(combo);
 112         }
 113 
 114         public boolean isFocusable() {
 115             return true;
 116         }
 117     }
 118 }