开发者

Not sure how to get an object to display after loading from ObjectInputStream()

public void load()
{
    final JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(this);
    if(returnVal 开发者_开发知识库== JFileChooser.APPROVE_OPTION)
    {
        try{
            FileInputStream f_in = new FileInputStream(fc.getSelectedFile());
            ObjectInputStream obj_in = new ObjectInputStream(f_in);
            Frame f2 = (Frame)obj_in.readObject();
            obj_in.close();
            f2.setVisible(true);
        }
        catch(Exception ex){
        }
    }
}


I'd try adding a call to the pack method on the frame before the setVisible call. This should cause it to be displayable and sized properly.

FileInputStream f_in = new FileInputStream(fc.getSelectedFile());
ObjectInputStream obj_in = new ObjectInputStream(f_in);
Frame f2 = (Frame)obj_in.readObject();
obj_in.close();
f2.pack()
f2.setVisible(true);

If this doesn't work, then check the return value of f2.isDisplayable() after the call to pack. If it is false, that means that the frame is not connected to a native screen resource (which I believe pack should take care of, but I'm uncertain of this in a deserialization scenario).

Another problem may be incompatibility between the JVM which serialized the frame and the one which is deserializing it if the frame includes Swing components. There are warnings in the javadocs for Swing components about switching JVM versions when using serialization for them.

In general, if you have a choice about serializing anything, it should only be the data model, not the view/presentation/GUI layer. So if it's an option, I'd avoid your current implementation approach entirely.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜