Solved

saving/loading JEditorPane as an object

Posted on 2002-05-21
4
534 Views
Last Modified: 2008-03-04
Hi there,
I have an application that has an JEditorPane area where I write text and draw basic shapes(circle, square). When saving the JEditorPane as an object to a file (after I typed some text)I have this error :
java.io.NotSerializableException: javax.swing.text.JTextComponent$InputMethodReq
uestsHandler

anyway if I paste some text and not typping it I can save the object along with the graphic (e.g. circle)

The problem is when I am loading back the object I can see just the text I saved and not the circle as well...

Why is that?
0
Comment
Question by:pelau
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7025335
How are you writing out the document? Can you post the relevant code?
0
 

Author Comment

by:pelau
ID: 7025386

[...]
//saving the jeditorpane area
//I have omited try-catch for easy understanding
MyJEditor jeditorPane = new MyJEditor();
//this myjeditor is a derived class from JEditorPane.
File fileName = fileChooser.getSelectedFile();
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(fileName));
objOut.writeObject(jeditorPane);
objOut.flush();


//and loading back the object:

MyJEditor mj = (MyJEditor)objIn.readObject();
c.add(mj, BorderLayout.CENTER);
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 100 total points
ID: 7025491
Well, I'm not sure what content type you've specified. But I don't think it really matters. Let me suggest that the way to write and read the content is to use the EditorKit associated with the JEditorPane. An EditorKit is responsible for supporting an appropriate Document model for a specific content type. An EditorKit is meant to specifically support one type of content, a String description of which should be retrievable with the getContentType() method. A corresponding Document instance should be returned by the createDefaultDocument() method, and the EditorKit should be able to read() and write() that Document to InputStreams/OutputStreams and Readers/Writers, respectively.

Example:

    File file = fileChooser.getSelectedFile();
    OutputStream out = new FileOutputStream(file);
    EditorKit kit = pane.getEditorKit();
    Document doc = pane.getDocument();
    kit.write(out, doc, 0, doc.length());
    out.flush();
    out.close();

This may still not solve your problem relative to images because the EditorKit for the content type may not support reading/writing images. RTF, I think, does not. But this is starting to stray into an area where I'm not sure I can help much.

Here are a few links to additional information resources that might prove helpful:
http://www.jalice.net/attributes.htm
http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html

I'll post others as I find them.
Jim
0
 

Author Comment

by:pelau
ID: 7035319
thanks, I will try the editorkit thing .
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now