• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing rtf

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Thanks,
Derrick

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 );
}
}
java.lang.NullPointerException
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)
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Derrick, your code is working with no exception on my computer.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic