Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6611596

Last Line in JTextPane is not showing

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6u2
    • client-libs

      With JTextPane the last line (last paragraph) is not showing. This bug occur with follow conditions:
       - The conten type must be change before.

       - The conten type must be text/plain

       - The caret position must be set to 0.
      workarounds:

       - create a new JTextPane if the conten type will be changed

       - does not set the caret to position 0

       - use JEditorPane instead of JTextPane

      See the follow test program to reprodce the problem. We have reproduce it with Java 5 and Java 6.

      import javax.swing.*;
      public class TestShowTextWithoutLastLine {
          public static void main( String[] args ){
              JFrame frame = new JFrame();
              frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );

              JTextPane text = new JTextPane();
              frame.add( text );
              String anyText = "first line\nsecond line";
              text.setContentType( "text/html" );
              text.setText( anyText );
              text.setContentType( "text/plain" );
              text.setText( anyText );
              text.setCaretPosition( 0 );

              frame.pack();
              frame.show();
          }
      }
      Further concern from the submitter and still think it is a bug:

      The evaluation field show a solution but the API documentation say that the method setText() is
      thread safe.
      The follow modified test program show that the problem occur inside of setText()
      if it is a thread problem.
      Only the method setText() is not call from the event thread and the
      problem repeat. The API doc says that
      this method is thread safe.

      import javax.swing.*;
      public class TestShowTextWithoutLastLine {

          public static void main( String[] args ) throws Exception, Exception{
              JFrame frame = new JFrame();
              frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
           
              final JTextPane text = new JTextPane();
            
              frame.add( text );
              frame.setSize(300,200);
              frame.show();
            
              final String anyText = "first line\nsecond line";
              SwingUtilities.invokeAndWait( new Runnable() {
                  public void run() {
                      text.setContentType( "text/html" );
                  }
              });
              text.setText( anyText );
              SwingUtilities.invokeAndWait( new Runnable() {
                  public void run() {
                      text.setContentType( "text/plain" );
                      text.setText( anyText );
                      text.setCaretPosition( 0 );
                  }
              });
          }
      }

            peterz Peter Zhelezniakov
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: