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 * @bug 8025082 27 * @summary The behaviour of the highlight will be lost after clicking the set 28 * button. 29 * @run main bug8025082 30 */ 31 import java.awt.*; 32 import java.awt.event.ActionEvent; 33 import java.awt.event.ActionListener; 34 import java.awt.event.InputEvent; 35 import javax.swing.*; 36 37 public class bug8025082 { 38 39 private static JButton button; 40 private static JFrame frame; 41 42 public static void main(String[] args) throws Exception { 43 Robot robo = new Robot(); 44 robo.delay(500); 45 46 SwingUtilities.invokeAndWait(new Runnable() { 47 public void run() { 48 createUI(); 49 } 50 }); 51 52 robo.waitForIdle(); 53 Point point = getButtonLocationOnScreen(); 54 robo.mouseMove(point.x, point.y); 55 robo.mousePress(InputEvent.BUTTON1_MASK); 56 robo.mouseRelease(InputEvent.BUTTON1_MASK); 57 robo.waitForIdle(); 58 59 SwingUtilities.invokeAndWait(new Runnable() { 60 public void run() { 61 frame.dispose(); 62 } 63 }); 64 } 65 66 private static void createUI() { 67 frame = new JFrame(); 68 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 69 frame.setSize(500, 500); 70 JTextPane textpane = new JTextPane(); 71 textpane.setText("Select Me"); 72 textpane.selectAll(); 73 74 JPanel panel = new JPanel(new BorderLayout()); 75 panel.add(textpane, BorderLayout.CENTER); 76 button = new JButton("Press Me"); 77 panel.add(button, BorderLayout.SOUTH); 78 79 button.addActionListener(new ActionListener() { 80 @Override 81 public void actionPerformed(ActionEvent e) { 82 if (!textpane.getCaret().isSelectionVisible()) { 83 throw new RuntimeException("Highlight removed after " 84 + "button click"); 85 } 86 } 87 }); 88 89 frame.getContentPane().add(panel); 90 frame.setLocationRelativeTo(null); 91 frame.setVisible(true); 92 } 93 94 private static Point getButtonLocationOnScreen() throws Exception { 95 final Point[] result = new Point[1]; 96 97 SwingUtilities.invokeAndWait(new Runnable() { 98 @Override 99 public void run() { 100 Point point = button.getLocationOnScreen(); 101 point.x += button.getWidth() / 2; 102 point.y += button.getHeight() / 2; 103 result[0] = point; 104 } 105 }); 106 return result[0]; 107 } 108 }