• Post Reply Bookmark Topic Watch Topic
  • New Topic

writing rtf  RSS feed

Derrick Koes
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running the code below produces the stack trace following the code listing.
I think I have to define a style somehow, but I'm not sure and I'm not sure how. I really want to parse an RTF file for the text content, but this is the closest I can get with the 1.4 API.

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;
class RTFView
extends JFrame
public RTFView()
setTitle( "RTF Text Application" );
setSize( 400, 240 );
setBackground( Color.gray );
getContentPane().setLayout( new BorderLayout() );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel, BorderLayout.CENTER );
// Create an RTF editor window
RTFEditorKit rtf = new RTFEditorKit();
JEditorPane editor = new JEditorPane();
editor.setEditorKit( rtf );
editor.setBackground( Color.white );
// This text could be big so add a scroll pane
JScrollPane scroller = new JScrollPane();
scroller.getViewport().add( editor );
topPanel.add( scroller, BorderLayout.CENTER );
// Load an RTF file into the editor
try {
FileInputStream fi = new FileInputStream( "C:/Documents and Settings/koes/My Documents/1252003.rtf" );
rtf.read( fi, editor.getDocument(), 0 );
catch( FileNotFoundException e )
System.out.println( "File not found" );
catch( IOException e )
System.out.println( "I/O error" );
catch( BadLocationException e )
public static void main( String args[] )
// Create an instance of the test application
RTFView mainFrame= new RTFView();
mainFrame.setVisible( true );
at javax.swing.text.rtf.RTFReader$StylesheetDestination$StyleDefiningDestination.close(RTFReader.java:914)
at javax.swing.text.rtf.RTFReader.setRTFDestination(RTFReader.java:247)
at javax.swing.text.rtf.RTFReader.handleKeyword(RTFReader.java:477)
at javax.swing.text.rtf.RTFParser.write(RTFParser.java:232)
at javax.swing.text.rtf.RTFParser.writeSpecial(RTFParser.java:101)
at javax.swing.text.rtf.AbstractFilter.write(AbstractFilter.java:158)
at javax.swing.text.rtf.AbstractFilter.readFromStream(AbstractFilter.java:88)
at javax.swing.text.rtf.RTFEditorKit.read(RTFEditorKit.java:65)
at com.snn.integration.RTFView.<init>(RTFView.java:37)
at com.snn.integration.RTFView.main(RTFView.java:55)
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Derrick, your code is working with no exception on my computer.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!