JTextPane

F

freesoft_2000

Hi everyone,

I am currently trying to serialize parts of a jtextpane
using the java ObjectOutputStream to write it to disk and when i am
reading the object back i am using the java ObjectInputStream.

When i read the api about the streams here is what they say

Warning: Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is appropriate
for short term storage or RMI between applications running the same
version of Swing. As of 1.4, support for long term storage of all
JavaBeansTM has been added to the java.beans package. Please see
XMLEncoder.

After reading this i decided to serialize my jtextpane objects using the
above
xml format but could not get it to work

I am providing a ruunable example so you guys can compile the source code
and see what i mean

Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;

public class XMLEcoderDecoder implements ActionListener, ItemListener
{

JFrame mainFrame = new JFrame("Frame");

JLabel labelerror = new JLabel("                              ",
SwingConstants.RIGHT);
JButton bLoad = new JButton("Load");
JButton bSaveAs = new JButton("Save as");
JButton bSave = new JButton("Save");
JButton bInsertPic = new JButton("Insert picture");
JButton bBackgroundColor = new JButton("Set background color");

JFileChooser selectedFile = new JFileChooser();
JFileChooser insertIconFile = new JFileChooser();

JColorChooser backgroundChooser = new JColorChooser();

Color backgroundColor;

JComboBox fontFamily;
JComboBox fontSize = new JComboBox();

JTextPane mainTextPane = new JTextPane();

SimpleAttributeSet sas = new SimpleAttributeSet();

StyleContext sc = new StyleContext();

MutableAttributeSet mas;

DefaultStyledDocument dse = new DefaultStyledDocument(sc);

JScrollPane mainScrollPane = new JScrollPane(mainTextPane,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

RTFEditorKit rtfkit = new RTFEditorKit();


Dimension Size1 = new Dimension();

String SF = "";

public void initialize ()
{
Container pane = mainFrame.getContentPane();
pane.setLayout(new FlowLayout());
mainFrame.setSize(250,300);
mainFrame.setLocation(300,300);
mainFrame.setBackground(Color.lightGray);
//The below command line is the JTextPane using the rtf editor kit
//for any rtf editing

mainTextPane.setEditorKit(rtfkit);
//The below command line sets the document that the JTextPane will be
//be referencing to

mainTextPane.setDocument(dse);
Size1.width = 500;
Size1.height = 300;
mainScrollPane.setPreferredSize(Size1);
pane.add(mainScrollPane);
pane.add(bLoad);
pane.add(bSaveAs);
pane.add(bSave);
pane.add(bInsertPic);
pane.add(bBackgroundColor);

combofontfamilyinitialize();
pane.add(fontFamily);
combofontsizeinitialize();
pane.add(fontSize);
pane.add(labelerror);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bLoad.addActionListener(this);
bSaveAs.addActionListener(this);
bSave.addActionListener(this);
bInsertPic.addActionListener(this);
bBackgroundColor.addActionListener(this);

fontFamily.addItemListener(this);
fontSize.addItemListener(this);
mainFrame.pack();
mainFrame.setVisible(true);
}

public void combofontfamilyinitialize ()
{
//This function fills the combo box with the system available font
families

GraphicsEnvironment ge1 =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] k = ge1.getAvailableFontFamilyNames();
fontFamily= new JComboBox(k);
}

public void combofontsizeinitialize ()
{
//This function fills the combo box with font sizes

fontSize.addItem("8");
fontSize.addItem("9");
fontSize.addItem("10");
fontSize.addItem("11");
fontSize.addItem("12");
fontSize.addItem("14");
fontSize.addItem("16");
fontSize.addItem("18");
fontSize.addItem("20");
fontSize.addItem("22");
fontSize.addItem("24");
fontSize.addItem("26");
fontSize.addItem("28");
fontSize.addItem("36");
fontSize.addItem("48");
fontSize.addItem("72");
}

public void setAttributeSet(AttributeSet attr)
{

//This function only set the specified font set by the
//attr variable to the text selected by the mouse

int xStart, xFinish, k;

xStart = mainTextPane.getSelectionStart();
xFinish = mainTextPane.getSelectionEnd();
k = xFinish - xStart;

if(xStart != xFinish)
{
dse.setCharacterAttributes(xStart, k, attr, false);
}

else if(xStart == xFinish)
{
//The below two command line updates the JTextPane according to what
//font that is being selected at a particular moment

mas =  rtfkit.getInputAttributes();
mas.addAttributes(attr);
}
//The below command line sets the focus to the JTextPane

mainTextPane.grabFocus();
}

public void open ()
{

if(selectedFile.showOpenDialog(mainFrame) !=
JFileChooser.APPROVE_OPTION)
{
return;
}

try
{
File file1 = selectedFile.getSelectedFile();
SF = file1.toString();
FileInputStream fStream = new FileInputStream(SF);
XMLDecoder stream = new XMLDecoder(fStream);

Object obj;
int i = 0;

//The below for loop reads in all the two objects required
//for the initialization of the objects in the program

for(i=0;i<2;i++)
{
//The below command line reads the first object in the file

obj = stream.readObject();

//The below command line initializes the first object in the file
//coresponding to the objects in the program

loadinitialize(obj);
}

stream.close();
fStream.close();
}

catch (Exception e)
{
labelerror.setText("A document reading error has occured");
}

}

public void loadinitialize(Object Object1)
{
//This function initilizes all the objects in the program according
//to the loading of a new file

if(Object1 instanceof DefaultStyledDocument)
{
//The document must be first read into the DefaultStyledDocument
//before any document rendering can be done to it

dse = ((DefaultStyledDocument)Object1);
mainTextPane.setDocument(dse);
}

else if(Object1 instanceof Color)
{
//The below command line loads the color as an object
//using the user specified file name and sets the color
//to the current JTextPane

mainTextPane.setBackground((Color)Object1);
}

}

public void saveas ()
{

if(selectedFile.showSaveDialog(mainFrame) !=
JFileChooser.APPROVE_OPTION)
{
return;
}

try
{
File file2 = selectedFile.getSelectedFile();
SF = (file2.toString() + ".xml");
FileOutputStream fStream = new FileOutputStream(SF);
XMLEncoder stream = new XMLEncoder(fStream);
stream.writeObject(mainTextPane.getDocument());
stream.writeObject(mainTextPane.getBackground());
stream.flush();
stream.close();
fStream.close();
}

catch (Exception e)
{
labelerror.setText("A document writing error has occured");
}

}

public void save ()
{

try
{
String str1 = SF;

if(str1.equals(""))
{
saveas();
return;
}

FileOutputStream fStream = new FileOutputStream(str1);
XMLEncoder stream = new XMLEncoder(fStream);
stream.writeObject(mainTextPane.getDocument());
stream.writeObject(mainTextPane.getBackground());
stream.flush();
stream.close();
fStream.close();
}

catch (Exception e)
{
labelerror.setText("A document writing error has occured");
}

}

public void actionPerformed(ActionEvent event)
{
JComponent b = (JComponent)event.getSource();
String str3 = null;

if(b == bLoad)
{
open();
}

else if(b == bSaveAs)
{
saveas();
}

else if(b == bSave)
{
save();
}

else if(b == bInsertPic)
{

insertIconFile.setDialogType(JFileChooser.OPEN_DIALOG);
insertIconFile.setDialogTitle("Select a picture to insert into
document");

if(insertIconFile.showDialog(mainFrame,"Insert") !=
JFileChooser.APPROVE_OPTION)
{
return;
}

File g = insertIconFile.getSelectedFile();
ImageIcon image1 = new ImageIcon(g.toString());
mainTextPane.insertIcon(image1);
mainTextPane.grabFocus();
}

else if(b == bBackgroundColor)
{

backgroundColor = backgroundChooser.showDialog(mainFrame, "Color
Chooser", Color.white);

if(backgroundColor != null)
{
mainTextPane.setBackground(backgroundColor);
}

}

}

public void itemStateChanged(ItemEvent event)
{
JComponent c = (JComponent)event.getSource();
boolean d;

if(c == fontFamily)
{

String j = (String)fontFamily.getSelectedItem();
StyleConstants.setFontFamily(sas, j);
setAttributeSet(sas);
}

else if(c == fontSize)
{

String h = (String)fontSize.getSelectedItem();
int r = Integer.parseInt(h);
StyleConstants.setFontSize(sas, r);
setAttributeSet(sas);
}

}
public static void main(String args[])
{
XMLEcoderDecoder a = new XMLEcoderDecoder();
a.initialize();
}
}

When i insert an icon put in some styled text and change the background
color of the jtextpane the file is written to disk with no exceptions
but when i try to read back the same file only the previously background
color is reloaded but the styled text and embedded icon are gone.

The thing is when i replace both the XMLEncoder and XMLDecoder with
ObjectOutputStream and ObjectInputStream respectively everything works
fine.

I don't know why can't serialize objects of my jtextpane but am i missing
something.

I hope someone can help me with this problem

Thank You

Yours Sincerely

Richard West
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top