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

RTFEditorKit writes background color but doesn't read

XMLWordPrintable

    • b10
    • x86
    • linux

      ADDITIONAL SYSTEM INFORMATION :
      java version "1.8.0_172"
      Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      JTextPane with RTFEditorKit displays background color for specific word(changed by the doc.setCharacterAttributes()), writes it into rtf file, but doesn't read it.

      The bug in the RTFEditor$AttributeTrackingDestination.handleKeyword(String keyword, int parameter) method: it stores "cf" keyword into "parserState", but doesn't store "cb" keyword. Just need to add :
              if (keyword.equals("cb")) {
                  parserState.put(keyword, Integer.valueOf(parameter));
                  return true;
              }


      REGRESSION : Last worked in version 8u202

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create JTextPane with RTFEditorKit
      2. Set YELLOW color as background color for some word
      3. Save RTF into file
      4. Read RTF from file and display it

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Background color of word should be YELLOW
      ACTUAL -
      Background color of word is WHITE

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.io.InputStream;
      import java.io.OutputStream;
      import java.nio.file.Files;
      import java.nio.file.Paths;

      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JTextPane;
      import javax.swing.text.MutableAttributeSet;
      import javax.swing.text.SimpleAttributeSet;
      import javax.swing.text.StyleConstants;
      import javax.swing.text.rtf.RTFEditorKit;

      public class tt {
          static JTextPane text;

          public static void main(String[] a) throws Exception {
              JFrame f = new JFrame();
              f.setBounds(200, 600, 400, 300);
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              text = new JTextPane();
              text.setEditorKit(new RTFEditorKit());

              MutableAttributeSet attrBackground = new SimpleAttributeSet();
              StyleConstants.setBackground(attrBackground, Color.YELLOW);
              MutableAttributeSet attrForeground = new SimpleAttributeSet();
              StyleConstants.setForeground(attrForeground, Color.GREEN);

              text.getDocument().insertString(0, "yellow_background\n", attrBackground);
              text.getDocument().insertString(0, "green_foreground ", attrForeground);
              write();
              read();

              JPanel p = new JPanel(new BorderLayout());
              p.add(text, BorderLayout.CENTER);
              p.add(new TinySwingPanel(text), BorderLayout.SOUTH);
              f.getContentPane().add(p);
              f.setVisible(true);
          }

          static void write() {
              try (OutputStream o = Files.newOutputStream(Paths.get("/tmp/test.rtf"))) {
                  text.getEditorKit().write(o, text.getDocument(), 0, 0);
              } catch (Exception e2) {
                  throw new RuntimeException();
              }
          }

          static void read() {
              try (InputStream in = Files.newInputStream(Paths.get("/tmp/test.rtf"))) {
                  text.getEditorKit().read(in, text.getDocument(), 0);
              } catch (Exception e2) {
                  throw new RuntimeException();
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: