Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JEditorPane.getText() with text/rtf

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
Hej,

My poor application has been working with a JEditorPane but after adding this:

edit.setContentType("text/rtf");

The following call has been unexpectedly returning null:

edit.getText();

Documentation says that .getText() returns the text contained in this TextComponent in terms of the content type of this editor.

What does it mean by 'in terms of' and why do you think I might be getting a null?

Many thanks! :)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Dunno. You could always download the JDK src and do a debug run into Sun's Swing code. This has helped me work through some of Swing's oddities in the past.

Tim
 
Do you have a link?

If someone could explain how to make this print "hello world" then I am sure all my problems would be solved.

Code:
import javax.swing.*;

class rtf {
  public static void main(String[] args){
	JEditorPane a = new JEditorPane();
	a.setContentType("text/rtf");
	a.setText("hello world");
	System.out.println( (a.getText() == null) ? "null" : a.getText() );
  }
}

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
I feel inclinded to add that text and text/html work fine!

There seems to be something fishy about text/rtf.

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Hi Dian,

Maybe it was not clear but I can read, set, and edit the rtf/text content. An RTF document created with WordPad displays correctly in the JEditorPane and can be edited.

What I cannot do is retrieve the content of the JEditorPane - in any capacity.

This means that my editor is currently only good for viewing existing RTF files, editing RTF data, and then saving an empty file! LOL

I have played around the RTFEditorKit and it has not assisted in retreiving the contents of the JEditorPane.

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Have you tried something like the following??

If this doesn't work, put some try, catch blocks for each statement that may be causing a problem.

Code:
class rtf {
  public static void main(String[] args){
    JEditorPane a = new JEditorPane();
    RTFEditorKit rtfKit = new RTFEditorKit();
    a.setEditorKit(rtfKit);
    a.setContentType("text/rtf");
    a.setText("hello world");
    System.out.println( (a.getText() == null) ? "null" : a.getText() );
  }
}


[monkey][snake] <.
 
That doesn't work. It's strange. I even tried to insert some Hello World in rft and doesn't work either.

Cheers,
Dian
 
Yeah, I came to the same conclusion. You append a new Document to the text pane, and then edit the document.

Actually, I dropped the RTF idea in favour of a Java native DefaultStyledDocument which gives something like this:
Code:
JTextArea textArea = new JTextArea(5, 20);
SimpleAttributeSet styles = new SimpleAttributeSet();
DefaultStyledDocument document = new DefaultStyledDocument();
textArea.setDocument(document);

StyleConstants.setForeground(styles, Color.red);
document.insertString(0, "hello world", styles);
Seems OK, styles are ignored?! OOOOO... Java!!!

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Update: Document and AttributeSet work with JTextPane.

They do not seem to work 'fully' with JTextArea or JEditorPane. This is frustrating because I do not want text-wrapping.

[banghead]

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
It is OK. All fixed now.

No stars this time but a big thankyou for trying :)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top