Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

saving a jtree...

Thread view: 
tiewknvc9 - 28 Dec 2005 15:45 GMT
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();
}
-----------------------------------------------------------------------------------------------
Roedy Green - 28 Dec 2005 17:26 GMT
>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:

You do know you can't do that in an unsigned Applet.
See http://mindprod.com/jgloss/applet.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 28 Dec 2005 17:29 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 28 Dec 2005 19:00 GMT
> 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
[quoted text clipped - 5 lines]
>     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);
            }
        });
    }
}
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Andrew Thompson - 29 Dec 2005 00:41 GMT
> Sub: saving a jtree...

Use less JPaper!

( sorry, I just couldn't resist that )

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.