saving a jtree...

T

tiewknvc9

Hi,

Im trying to save a MyJTree class by serializing the class and of
course writing the output to a file.... but every time I try to save
it I get an error:

Exception in thread "AWT-EventQueue-0" java.lang.InternalError:
incorrect component
at javax.swing.plaf.basic.BasicTreeUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)


any ideas? code follows....


when the saved button is pressed this happens:
---------------------------------------------
if (e.getActionCommand().equals("mniSave")){ //save the 'file'
try{
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("C:\\test.txt"));
out.writeObject(m_tlTree);
out.flush();
out.close();
}catch (IOException ioe){
System.out.println("Failed to save");
}
-----------------------------------------

my jtree class implements serializable and has this code as writeobject
and readobject...

------------------------------------------------------------------------------------------
// --------- Serializable --------------
//for saving
private void writeObject(java.io_ObjectOutputStream out)
throws IOException {

out.defaultWriteObject();
}

private void readObject(java.io_ObjectInputStream in)
throws IOException, ClassNotFoundException {

in.defaultReadObject();
}
-----------------------------------------------------------------------------------------------
 
R

Roedy Green

Exception in thread "AWT-EventQueue-0" java.lang.InternalError:
incorrect component
at javax.swing.plaf.basic.BasicTreeUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)

Try putting in some debug code. Your problem may have nothing to do
with serializing. Try System.out.println("here");
just before you start to serialize.
 
T

Thomas Hawtin

tiewknvc9 said:
Im trying to save a MyJTree class by serializing the class and of
course writing the output to a file.... but every time I try to save
it I get an error:

Exception in thread "AWT-EventQueue-0" java.lang.InternalError:
incorrect component
at javax.swing.plaf.basic.BasicTreeUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)

Swing components uninstall their UI delegate when serialising. Possibly
it is a problem relating to that (I don't think it is tolerant of
exceptions, for instance).

I've tried to reproduce your problem with no lock. My code is below.
Does it work for you? Can you make it not work (in the appropriate style)?

Tom Hawtin


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyTree extends JTree {
private void writeObject(
ObjectOutputStream out
) throws IOException {
out.defaultWriteObject();
}
private void readObject(
ObjectInputStream in
) throws IOException, ClassNotFoundException {
in.defaultReadObject();
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("SerialTree");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
final JTree tree = new MyTree();
frame.add(tree);
JButton save = new JButton("Save");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
OutputStream fileOut =
new FileOutputStream("test.ser");
try {
ObjectOutputStream objectOut =
new ObjectOutputStream(
fileOut
);
objectOut.writeObject(tree);
objectOut.flush();
} finally {
fileOut.close();
}
} catch (IOException exc) {
exc.printStackTrace();
}
}
});
frame.add(save, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
});
}
}
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top