简体   繁体   English

无法序列化JTree的根节点(groovy / java)

[英]Unable to serialize a root node of a JTree (groovy/java)

I am working on a swing application which primarily includes a JTree and some other components in it. 我正在开发一个主要包含JTree和其他组件的swing应用程序。 The complete logic behind the UI display is based upon the root node of JTree. UI显示背后的完整逻辑基于JTree的根节点。 It is a nested node with individual custom UserObjects set to each of the child nodes. 它是一个嵌套节点,每个子节点都有单独的自定义UserObjects。

I need to preserve the state of my application for which the single nested root node of the JTree should be preserved. 我需要保留应保留JTree的单个嵌套根节点的应用程序的状态。 I am unable to do so. 我无法这样做。

class SerializeImpl implements Serializable{

    def doSerialize() throws Exception{
        def root = FeedTree.getInstance().getModel().getRoot()
        def object = new SerializableNode(top:root)
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("new.txt"))
        out.writeObject(object)
    }

    def doDeSerialize(){
        def file = new File('new.txt')
        def serNodeObj
        try{        
            file.withObjectInputStream(getClass().classLoader){ ois ->
                ois.eachObject{ serNodeObj = it }
            }
            return serNodeObj.getValue()
        }
        catch(FileNotFoundException ex){
            return null
        }

    }       

}


class SerializableNode implements Serializable{

    def top

    def getValue(){
        return top
    }

}

class FeedTree extends JTree{

...............
a singleton instance
...............

}

The doSerialize() method is executed first followed by a System.exit(1) and followed by a fresh display of UI which does doDeSerialize() .. The doSerialize() method does write something onto the news.txt file but I am not sure if it is serializing the object corrrectly. 首先执行doSerialize()方法,然后执行System.exit(1) ,然后执行UI的全新显示,该UI确实执行doDeSerialize()doSerialize()方法确实将一些内容写入了news.txt文件,但我没有确保它正确地序列化了对象。 Moreover the System.exit(1) after serializing doesn't work. 此外,序列化后的System.exit(1)不起作用。

After a forced Exit (from eclipse console close) the first execution of doDeSerialize() throws the following exception. 在强制退出(从Eclipse控制台关闭)后,第一次执行doDeSerialize()会引发以下异常。

Caught: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: groovy.util.slurpersupport.NodeChildren
    at functionalities.SerializeImpl$_doDeSerialize_closure1.doCall(SerializeImpl.groovy:23)
    at functionalities.SerializeImpl.doDeSerialize(SerializeImpl.groovy:22)

I am unable to understand why the serialize is (probably) failing and why the System.exit(1) is not functioning correctly after the serialize. 我无法理解为什么序列化(可能)失败,以及为什么System.exit(1)在序列化后无法正常运行。 Please help. 请帮忙。

In order for SerializableNode to actually be serializable, its whole object graph must be serializable. 为了使SerializableNode实际上可序列化,其整个对象图必须可序列化。 What type of value is top ? 什么类型的价值是top If that is not serializable, the node will thrown a NotSerializableException when writing. 如果那是不可序列化的,则该节点在写入时将引发NotSerializableException

If top cannot be made Serializable, then you should declare it as transient and implement writeResolve and readResolve to properly write/read the non serializable value. 如果不能将top设置为Serializable,则应将其声明为transient并实现writeResolvereadResolve以正确写入/读取不可序列化的值。

look at method invokeLater or better for Serializable for invokeAndWait() , because Swing code must be done on Event Dispatch Thread, 为invokeAndWait()查看方法invokeLater或更好的Serializable方法,因为必须在事件调度线程上完成Swing代码,

correct way, create JTree with DefaultTreeModel separatelly, and from your Serializable methods just to add TreeNodes wrapped into invokeLater 正确的方法是,分别使用DefaultTreeModel创建JTree ,并从Serializable方法创建JTree ,仅添加包装在invokeLater TreeNodes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM