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