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

Clicking on JTextPane containing styled DefaultStyleDocument causes style bleed

XMLWordPrintable

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

      FULL PRODUCT VERSION :
      java version "1.6.0_02"
      Java(TM) SE Runtime Environment (build 1.6.0_02-b05)
      Java HotSpot(TM) Client VM (build 1.6.0_02-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      As displayed in the steps to reproduce below, this bug involves:

      1. styling a DefaultStyledDocument the very last character
      2. clicking the JTextPane to which this document is assigned
      3. swapping in other DefaultStyledDocuments into the same JTextPane

      Doing this causes the style applied in 1, to bleed to all other StyledDocuments that will be used with that same JTextPane.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Launch the code provided
      2. Click the next button til you get to the last message
      3. Click anywhere on the JTextPane in the JFrame
      4. Hit the previous button to view other messages

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The unstyled first, and partially styled second messages would display with their appropriate components styled
      ACTUAL -
      The entire first and second messages appear with the style applied by the third message

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import javax.swing.text.*;

      import java.awt.*;
      import java.awt.event.*;
      import java.util.regex.*;

      public final class MessageViewer extends JFrame {
      private static final long serialVersionUID = 1L;
      private static final Pattern PATTERN_URL = Pattern.compile( "https?://([-\\w\\.]+)+(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)?", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE );
      private static final Integer LINK_ATTRIBUTE = 1;

      public final String messages [] = { "This is a first message, ok to click",
      "http://google.com (clicking here is fine)",
      "clicking this message causes the bleed http://google.com" };

      private JTextPane outputScreen;
      private JScrollPane outputScrollPane;
      private int messageIndex;
      private JButton next, prev;

      public static void main( String args[] ){
      MessageViewer m = new MessageViewer();
      m.setVisible( true );
      }

      public MessageViewer(){
      super( "Message Viewer Test" );
      setLayout( new BorderLayout() );

      messageIndex = 0;

      outputScreen = new JTextPane();


      outputScreen.setEditable( false );
      outputScrollPane = new JScrollPane( outputScreen, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
      add( outputScrollPane, BorderLayout.CENTER );

      final JPanel buttonPanel = new JPanel( new FlowLayout( FlowLayout.RIGHT ) );
      next = new JButton( new NextMessageAction() );
      prev = new JButton( new PreviousMessageAction() );
      buttonPanel.add( prev );
      buttonPanel.add( next );
      add( buttonPanel, BorderLayout.AFTER_LAST_LINE );

      setSize( 500, 500 );
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      display( messages[0] );
      indexFix();
      }

      /**
      * Display a string, highlight the URLs
      * @param str
      */
      public void display( final String str ){

      DefaultStyledDocument doc = new DefaultStyledDocument();
      outputScreen.setStyledDocument( doc );
      outputScreen.setText( str );
      Matcher matcher = PATTERN_URL.matcher( str );

      Style s;
      while( matcher.find () ){
      s = doc.addStyle( "link" + matcher.start(), null );
      s.addAttribute( LINK_ATTRIBUTE, matcher.group() );
      StyleConstants.setForeground( s, Color.blue );
      StyleConstants.setBold( s, true );
      doc.setCharacterAttributes( matcher.start(), matcher.end() - matcher.start(), s, true );
      }
      }

      /**
      * Enable/disable buttons
      */
      public void indexFix(){
      next.setEnabled( messageIndex + 1 != messages.length );
      prev.setEnabled( messageIndex != 0 );
      }

      private class NextMessageAction extends AbstractAction{
      private static final long serialVersionUID = 1L;

      public NextMessageAction(){
      putValue( Action.NAME, "Next Message" );
      }

      public void actionPerformed( ActionEvent e ){
      display( messages[ ++messageIndex ] );
      indexFix();
      repaint();
      }
      }

      private class PreviousMessageAction extends AbstractAction{
      private static final long serialVersionUID = 1L;

      public PreviousMessageAction(){
      putValue( Action.NAME, "Previous Message" );
      }

      public void actionPerformed( ActionEvent e ){
      display( messages[ --messageIndex ] );
      indexFix();
      repaint();
      }
      }
      }
      ---------- END SOURCE ----------

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: