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 /*
  27  * @test
  28  * @key headful
  29  * @bug 6406264
  30  * @summary Tests that JComboBox's focusable popup can be shown.
  31  * @author Mikhail Lapshin
  32  * @run main bug6406264
  33  */
  34 
  35 import javax.swing.JComboBox;
  36 import javax.swing.JFrame;
  37 import javax.swing.SwingUtilities;
  38 import javax.swing.plaf.basic.BasicComboBoxUI;
  39 import javax.swing.plaf.basic.ComboPopup;
  40 import javax.swing.plaf.basic.BasicComboPopup;
  41 import java.awt.*;
  42 
  43 public class bug6406264 extends JComboBox {
  44 
  45     static JFrame frame;
  46     static bug6406264 comboBox;
  47 
  48     public static void main(String[] args) throws Exception {
  49 
  50         Robot robot = new Robot();
  51         // Setup and show frame
  52         SwingUtilities.invokeAndWait(
  53                 new Runnable() {
  54                     public void run() {
  55                         frame = new JFrame("JComboBox6406264 test");
  56                         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57 
  58                         comboBox = new bug6406264("One", "Two", "Three");
  59                         frame.getContentPane().add(comboBox);
  60 
  61                         frame.setLocationRelativeTo(null);
  62                         frame.pack();
  63                         frame.setVisible(true);
  64                     }
  65                 }
  66         );
  67 
  68         robot.waitForIdle();
  69 
  70         // Show popup
  71         SwingUtilities.invokeAndWait(
  72                 new Runnable() {
  73                     public void run() {
  74                         comboBox.showPopup();
  75                     }
  76                 });
  77         robot.waitForIdle();
  78 
  79         // Check that popup is visible
  80         SwingUtilities.invokeAndWait(
  81                 new Runnable() {
  82                     public void run() {
  83                         if (comboBox.getUI().isPopupVisible(comboBox) == false)
  84                         {
  85                             throw new RuntimeException("A focusable popup is not visible " +
  86                                     "in JComboBox!");
  87                         }
  88                     }
  89                 }
  90         );
  91 
  92         frame.dispose();
  93     }
  94 
  95     public bug6406264(Object ... items) {
  96         super(items);
  97     }
  98 
  99     public void updateUI() {
 100         setUI(new CustomComboBoxUI());
 101     }
 102 
 103     // Use FocusablePopup for our JComboBox
 104     private class CustomComboBoxUI extends BasicComboBoxUI {
 105         protected ComboPopup createPopup() {
 106             return new FocusablePopup(bug6406264.this);
 107         }
 108     }
 109 
 110     // Popup window which can take a focus
 111     private class FocusablePopup extends BasicComboPopup {
 112         public FocusablePopup(JComboBox combo) {
 113             super(combo);
 114         }
 115 
 116         public boolean isFocusable() {
 117             return true;
 118         }
 119     }
 120 }