The Artima Developer Community
Sponsored Link

Java Answers Forum
RTF Editor / Writer and LoadRtf object.

1 reply on 1 page. Most recent reply: Mar 8, 2003 3:15 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Genty hhgjv

Posts: 1
Nickname: gentyjp
Registered: Feb, 2003

RTF Editor / Writer and LoadRtf object. Posted: Feb 27, 2003 12:53 AM
Reply to this message Reply
Advertisement
Hi

I search for a sample RTF Editor to study RTF editing in Java.
I found one really interesting in legacy forum java answer, at URL http://www.artima.com/legacy/answers/Feb2002/messages/153.html.

I tried to compile it but it can't find LoadRtf object.

Could someone tell me where can I find it ?

Thanks for your help.

Jean-Paul


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: RTF Editor / Writer and LoadRtf object. Posted: Mar 8, 2003 3:15 PM
Reply to this message Reply
Try the RTFReader.java code at:
http://www.quantumhyperspace.com/SourceBank/viewCode.jsp?javaFile=RTFReader.java
or the following:

/* RTFViewer.java
* March 9, 2003
*/

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;


/** Constructs an RTF editor kit to display the
* contents of an RTF document in a JEditorPane.
* @author: Charles Bell
* @version March 8, 2003
*/
public class RTFViewer extends JFrame{

private RTFEditorKit kit;
private File file;
private JEditorPane editor;

/** Default constructor. */
public RTFViewer(){
super("RTF Viewer");
init();
}


/** Constructor using file object as input. */
public RTFViewer (File file){
super("RTF Viewer: " + file.getName());
this.file = file;
init();
}

/** Constructor using filename string object as input. */
public RTFViewer (String fileName){
super("RTF Viewer: " + fileName);
this.file = new File(fileName);
init();
}

/** Runs as application. */
public static void main(String[] args){
if (args.length == 0){
new RTFViewer();
}else{
new RTFViewer(args[0]);
}
}

/** Creates a default document to hold the contents of the RTF
* file, and displays its contents in a JEditorPane.
*/
public void init(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
kit = new RTFEditorKit();
editor = new JEditorPane();
editor.setEditable(false);
kit.install(editor);
Document doc = kit.createDefaultDocument();
if (file == null) {
file = getFile();
if (file == null) {
exit();
}
}
setTitle("RTF Viewer: " + file.getName());
FileInputStream fis = new FileInputStream(file);
kit.read(fis,doc,0);
editor.setText(doc.getText(0,doc.getLength()));
setSize(Toolkit.getDefaultToolkit().getScreenSize());
getContentPane().add(new JScrollPane(editor), BorderLayout.CENTER);
show();
}catch (BadLocationException ble){
System.err.println("BadLocationException: " + ble.getMessage());
}catch (IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}

/** Uses a JFileDialog to retrieve a file from the user.
*/
private File getFile(){
File f = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new RTFFileNameFilter());
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
f = fileChooser.getSelectedFile();
}
return f;
}

/** Control exit. */

private void exit(){
System.exit(0);
}

/** For the rtf file filter.
*/
class RTFFileNameFilter extends javax.swing.filechooser.FileFilter{

public boolean accept(File file){
boolean status = false;
String filename = file.getName().toLowerCase();
return (filename.indexOf("rtf") > 0);
}

public String getDescription(){
return "RTF file";
}
}

}

Flat View: This topic has 1 reply on 1 page
Topic: COOL Java Menu Previous Topic   Next Topic Topic: Word Frequency Data

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use