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 6866751
  26    @summary J2SE_Swing_Reg: the caret disappears when moving to the end of the line.
  27    @author Semyon Sadetsky
  28   */
  29 import javax.swing.*;
  30 import java.awt.*;
  31 
  32 public class bug6866751 {
  33     private static JFrame frame;
  34     private static JTextArea area;
  35 
  36     public static void main(String[] args) throws Exception {
  37         try {
  38             SwingUtilities.invokeAndWait(new Runnable() {
  39                 public void run() {
  40                     frame = new JFrame();
  41                     frame.setUndecorated(true);
  42                     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43                     setup(frame);
  44                 }
  45             });
  46             SwingUtilities.invokeAndWait(new Runnable() {
  47                 public void run() {
  48                     int width = area.getWidth();
  49                     double caretX =
  50                             area.getCaret().getMagicCaretPosition().getX();
  51                     if (width < caretX + 1) {
  52                         throw new RuntimeException(
  53                                 "Width of the area (" + width +
  54                                         ") is less than caret x-position " +
  55                                         caretX + 1);
  56                     }
  57                     area.putClientProperty("caretWidth", 10);
  58                     frame.pack();
  59                 }
  60             });
  61             new Robot().waitForIdle();
  62             SwingUtilities.invokeAndWait(new Runnable() {
  63                 public void run() {
  64                     int width = area.getWidth();
  65                     double caretX =
  66                             area.getCaret().getMagicCaretPosition().getX();
  67                     if (width < caretX + 10) {
  68                         throw new RuntimeException(
  69                                 "Width of the area (" + width +
  70                                         ") is less  than caret x-position " +
  71                                         caretX + 10);
  72                     }
  73                 }
  74             });
  75             System.out.println("ok");
  76         } finally {
  77             SwingUtilities.invokeAndWait(new Runnable() {
  78                 @Override
  79                 public void run() {
  80                     frame.dispose();
  81                 }
  82             });
  83         }
  84     }
  85 
  86     static void setup(JFrame frame) {
  87         area = new JTextArea();
  88         frame.getContentPane().add(new JScrollPane(area));
  89         area.setText(
  90                 "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm");
  91         area.getCaret().setDot(area.getText().length() + 1);
  92 
  93         frame.setSize(300, 200);
  94         frame.setVisible(true);
  95 
  96         area.requestFocus();
  97 
  98     }
  99 
 100 }