反序列化的JPanel组件丢失了它们的事件监听器

时间:2014-06-09 17:53:24

标签: java swing serialization netbeans

我正在开发一个swing项目,该项目涉及用户保存他正在使用的当前JPanel的状态,并且能够将其加载回选项卡式窗格中的另一个选项卡上,下图显示了应用程序的界面:

Image show the application's initial interface

在运行应用程序时,根据用户对现有组件的操作动态地将更新的组件添加到当前面板,这意味着这些现有组件以及更新的组件具有事件侦听器并且它们正在工作。见下图:

Image showing changes in the interface based on user's actions

如您所见,选项卡窗格中有不同的面板,当用户单击文件菜单中的保存菜单项时,当前面板将被序列化,并且它将被加载回选项卡式窗格的选项卡上标题为“#34;加载保存的算法"。

我学到了很难的方法,我将无法序列化使用NetBeans拖放设计的JPanel。删除GUI构建器,因为NetBeans使用的布局管理器是GroupLayout,而GroupLayout遗憾的是不可序列化!

但是我将应用程序设计得几乎到了最后阶段,所以更改布局管理器不是一个选择。我在SO搜索解决方案并发现了post,其中讨论了XStream

所以我决定使用XStream,我按照他们的教程进行序列化和反序列化成功查看下图:

Image showing loaded panel

现在的问题是,在反序列化后,我发现组件不再响应用户输入/事件。我需要帮助,或者是序列化非可序列化对象的另一种更好的替代方案,或者解决明显的问题。

以下是我的序列化代码:

public void savePanel() throws IOException
{
    int selectedIndex = this.parentTabPan.getSelectedIndex();
    JPanel panelTobeSaved = new JPanel();
    switch(selectedIndex)
    {
        case 0:
            panelTobeSaved = this.linearTabPanel;
            break;
        case 1:
            panelTobeSaved = this.quadraticTabPanel;
            break;
        case 2:
            panelTobeSaved = this.unconstranedTabPanel;
            break;
        case 3:
            Toolkit.getDefaultToolkit().beep();
            JOptionPane.showMessageDialog(this, "Please select an algorithm tab.", "Access Denied", JOptionPane.INFORMATION_MESSAGE);
            break;
        case 4:
            panelTobeSaved = this.loadSavedPanel;
            break;
    }

    int returnVal = fileChooser.showSaveDialog(this);
    if(returnVal == JFileChooser.APPROVE_OPTION) 
    {
        File file = fileChooser.getSelectedFile();
        File newFile = null;
        String fileName = file.getName();
        if(!fileName.contains(".") && fileChooser.getFileFilter() instanceof FileNameExtensionFilter)
        {
            FileNameExtensionFilter filter = (FileNameExtensionFilter)fileChooser.getFileFilter();
            newFile = new File(file.getPath()+"."+(filter.getExtensions())[0]);
            fileName = newFile.getName();
            System.out.println(fileName);
        }
        else
        {
            newFile = file;
            System.out.println(newFile.getName());
        }
        System.out.println(newFile.getAbsolutePath());
        try
        {
            XStream xstream = new XStream();
            String xml = xstream.toXML(panelTobeSaved);
            // if file doesn't exists, then create it
            if(!newFile.exists())
                newFile.createNewFile();
            try(FileWriter fw = new FileWriter(newFile.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter out = new PrintWriter(bw);)
            {
                out.print(xml);
            }
        }
        catch(IOException xcp)
        {
            Toolkit.getDefaultToolkit().beep();
            JOptionPane.showMessageDialog(this, xcp.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
            xcp.printStackTrace(System.err);
        }
    }
}

和反序列化:

private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt)
{
    if(evt.getSource() == this.openMenuItem)
    {
        int returnValue = this.fileChooser.showOpenDialog(this);
        if(returnValue == JFileChooser.APPROVE_OPTION)
        {
            File file = this.fileChooser.getSelectedFile();
            try(FileInputStream fstream = new FileInputStream(file.getAbsolutePath());
            DataInputStream in = new DataInputStream(fstream);)
            {
                byte[] data = new byte[in.available()];
                in.read(data);
                String xml = new String(data);
                XStream xstream = new XStream();
                JPanel restoredPanel = (JPanel)xstream.fromXML(xml);
                this.parentTabPan.removeTabAt(4);
                this.parentTabPan.addTab("Load Saved Algorithm", restoredPanel);
                this.parentTabPan.setSelectedIndex(4);
            }
            catch(IOException xcp)
            {
                Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(this, xcp.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
                xcp.printStackTrace(System.err);
            }
        }
    }
}

0 个答案:

没有答案