Win a copy of Kotlin in Action this week in the Kotlin forum!
  • 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 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" ); 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(
at javax.swing.text.rtf.RTFReader.setRTFDestination(
at javax.swing.text.rtf.RTFReader.handleKeyword(
at javax.swing.text.rtf.RTFParser.write(
at javax.swing.text.rtf.RTFParser.writeSpecial(
at javax.swing.text.rtf.AbstractFilter.write(
at javax.swing.text.rtf.AbstractFilter.readFromStream(
at com.snn.integration.RTFView.<init>(
at com.snn.integration.RTFView.main(
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.
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!