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