1 /*
   2  * Copyright (c) 2015, 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 /* @test
  25    @bug 8041470
  26    @summary JButtons stay pressed after they have lost focus if you use the mouse wheel
  27    @author Anton Nashatyrev
  28 */
  29 import javax.swing.*;
  30 import java.awt.*;
  31 import java.awt.event.*;
  32 import java.util.concurrent.CountDownLatch;
  33 
  34 public class WheelModifier {
  35 
  36     JFrame f;
  37     JButton fb;
  38 
  39     CountDownLatch pressSema = new CountDownLatch(1);
  40     CountDownLatch exitSema = new CountDownLatch(1);
  41     CountDownLatch releaseSema = new CountDownLatch(1);
  42     volatile CountDownLatch wheelSema;
  43 
  44     void createGui() {
  45         f = new JFrame("frame");
  46         fb = new JButton("frame_button");
  47         fb.addMouseListener(new MouseAdapter() {
  48             @Override
  49             public void mouseReleased(MouseEvent e) {
  50                 System.out.println("WheelModifier.mouseReleased: " + e);
  51                 releaseSema.countDown();
  52             }
  53 
  54             @Override
  55             public void mouseEntered(MouseEvent e) {
  56                 System.out.println("WheelModifier.mouseEntered: " + e);
  57 
  58             }
  59 
  60             @Override
  61             public void mouseExited(MouseEvent e) {
  62                 System.out.println("WheelModifier.mouseExited: " + e);
  63                 exitSema.countDown();
  64             }
  65 
  66             @Override
  67             public void mousePressed(MouseEvent e) {
  68                 System.out.println("WheelModifier.mousePressed: " + e);
  69                 pressSema.countDown();
  70             }
  71 
  72             @Override
  73             public void mouseDragged(MouseEvent e) {
  74                 System.out.println("WheelModifier.mouseDragged: " + e);
  75             }
  76         });
  77 
  78         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  79             @Override
  80             public void eventDispatched(AWTEvent event) {
  81                 System.out.println("WheelModifier.mouseWheel: " + event);
  82                 wheelSema.countDown();
  83             }
  84         }, MouseEvent.MOUSE_WHEEL_EVENT_MASK);
  85 
  86         f.setLayout(new FlowLayout());
  87         f.add(fb);
  88         f.setSize(200, 200);
  89         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  90         f.setVisible(true);
  91     }
  92 
  93     void run() throws Exception {
  94         Robot r = new Robot();
  95         r.waitForIdle();
  96         System.out.println("# Started");
  97 
  98         Point sLoc = fb.getLocationOnScreen();
  99         Dimension bSize = fb.getSize();
 100         r.mouseMove(sLoc.x + bSize.width / 2, sLoc.y + bSize.height / 2);
 101         r.mousePress(MouseEvent.BUTTON1_MASK);
 102         pressSema.await();
 103         System.out.println("# Pressed");
 104 
 105         r.mouseMove(sLoc.x + bSize.width / 2, sLoc.y + bSize.height * 2);
 106         exitSema.await();
 107         System.out.println("# Exited");
 108 
 109         wheelSema = new CountDownLatch(1);
 110         r.mouseWheel(1);
 111         wheelSema.await();
 112         System.out.println("# Wheeled 1");
 113 
 114         wheelSema = new CountDownLatch(1);
 115         r.mouseWheel(-1);
 116         wheelSema.await();
 117         System.out.println("# Wheeled 2");
 118 
 119         r.mouseRelease(MouseEvent.BUTTON1_MASK);
 120         releaseSema.await();
 121         System.out.println("# Released!");
 122     }
 123 
 124     public static void main(String[] args) throws Exception {
 125         WheelModifier test = new WheelModifier();
 126 
 127         SwingUtilities.invokeAndWait(() -> test.createGui());
 128         test.run();
 129 
 130         System.out.println("Done.");
 131     }
 132 }