2 Replies Latest reply: Jul 25, 2009 4:11 AM by 800308 RSS

    RTFEditorKit produce NumberFormatException

    843789
      I need to convert rtf content to StyledDocument. I am using

      String message = "\test-java";
      StringReader sread = new StringReader(message);
                          StyledDocument std = new DefaultStyledDocument();
                          RTFEditorKit kit = new RTFEditorKit();
                          kit.read(sread, std, 0);

      But this is not working fine. This code produce java.lang.NumberFormatException: For input string: "-". I dont know what bug is this.
        • 1. Re: RTFEditorKit produce NumberFormatException
          843789
          '\' is a meta character (an 'escape' character to modify the meaning of the next character) in String literals and makes '\t' the 'tab' character. You need
          String message = "\\test-java";
          The '\\' means 'escape the escape' and create a single real '\'.
          • 2. Re: RTFEditorKit produce NumberFormatException
            800308
            You're doing it wrong, apparently, first create your own RTFEditorKitTest.rtf using wordpad, then run this...
            package forums;
            
            // from http://www.codeguru.com/forum/showthread.php?t=33892
            
            import java.io.FileInputStream;
            import java.io.IOException;
            import java.awt.BorderLayout;
            import java.awt.event.WindowAdapter;
            import java.awt.event.WindowEvent;
            import javax.swing.JFrame;
            import javax.swing.JTextPane;
            import javax.swing.JScrollPane;
            import javax.swing.text.DefaultStyledDocument;
            import javax.swing.text.BadLocationException;
            import javax.swing.text.rtf.RTFEditorKit;
            
            public class RTFEditorKitTest extends JFrame
            {
              private static final long serialVersionUID = 1L;
            
              public RTFEditorKitTest() throws IOException, BadLocationException {
            
                JTextPane textpane = new JTextPane();
                getContentPane().add(new JScrollPane(textpane), BorderLayout.CENTER);
            
                textpane.setContentType("text/rtf");
                RTFEditorKit kit = (RTFEditorKit) textpane.getEditorKitForContentType("text/rtf");
                DefaultStyledDocument doc = (DefaultStyledDocument) kit.createDefaultDocument();
                textpane.setEditorKit(kit);
                textpane.setDocument(doc);
                FileInputStream input = null;
                try {
                  input = new FileInputStream("RTFEditorKitTest.rtf");
                  kit.read(input, doc, 0);
                } finally {
                  if (input!=null)input.close();
                }
                textpane.setDocument(doc);
            
                addWindowListener(
                  new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                      System.exit(0);
                    }
                  }
                );
              }
            
              public static void main(String args[]) {
                try {
                  JFrame frame = new RTFEditorKitTest();
                  frame.setBounds(300,300,650,450);
                  frame.setVisible(true);
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            }
            Cheers. Keith.