在GUI反序列化期间摆动触发侦听器

时间:2013-06-23 22:56:40

标签: java swing serialization serializable

今天我遇到了一个非常讨厌的错误,它涉及使用标准Java序列化API在GUI反序列化期间触发swing监听器。简明扼要地解释如何复制这种行为有点困难,所以我在下面发布了一个小测试用例。此测试用例不会抛出任何异常,触发任何编译器警告,当然似乎没有明确定义的行为。 这是一个错误,Oracle的软件包互操作文档中的灰色区域,还是之前尝试过的东西?

为了阐明我原来的用例,我试图在从磁盘重新加载配置组件时自动更新资产编辑器中的几个选项卡。

package com.ihateswing.ssce;

import java.awt.BorderLayout;

public class SSCE implements Serializable {



    private class Internal extends JPanel {
        private final JComboBox<String> m_cb = new JComboBox<String>();

        Internal(final JComboBox<String> cb) {
            cb.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    DefaultComboBoxModel<String> dcbm = new DefaultComboBoxModel<String>();
                    for (int i = 0; i < cb.getModel().getSize(); ++i) {
                        dcbm.addElement(cb.getModel().getElementAt(i));
                    }
                    m_cb.setModel(dcbm);
                }
            });
            super.add(m_cb);
        }
    }

    private JFrame frame;
    private JComboBox<String> jce = new JComboBox<String>();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SSCE window = new SSCE();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public SSCE() {
        initialize();
    }

    private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        jce.setSelectedIndex(0); // <-- Seems to have undefined behavior?
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jce.setModel(new DefaultComboBoxModel<String>());
        frame.getContentPane().add(new Internal(jce), BorderLayout.NORTH);
        frame.getContentPane().add(jce, BorderLayout.CENTER);

        JButton btnAddRandomItem = new JButton("Break");
        btnAddRandomItem.addActionListener(new AbstractAction() {
            private static final long serialVersionUID = 1L;
            private int i;

            public void actionPerformed(ActionEvent e) {
                try {
                    ((DefaultComboBoxModel<String>) jce.getModel())
                            .addElement(String.valueOf(i++));
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    ObjectOutputStream out = new ObjectOutputStream(baos);
                    out.writeObject(SSCE.this);
                    ByteArrayInputStream in = new ByteArrayInputStream(baos
                            .toByteArray());
                    ObjectInputStream oin = new ObjectInputStream(in);
                    oin.readObject();

                    // The line below updates 'Internal' as expected, uncomment
                    // to see...
                    // however with the listener fired from the serialization
                    // method, it breaks?
                    // jce.setSelectedIndex(0);
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        });
        frame.getContentPane().add(btnAddRandomItem, BorderLayout.SOUTH);
    }

}

带输出的样本(单击'中断'5次)

package com.ihateswing.ssce;

import java.awt.BorderLayout;

public class SSCE implements Serializable {



    private class Internal extends JPanel {
        private final JComboBox<String> m_cb = new JComboBox<String>();

        Internal(final JComboBox<String> cb) {
            cb.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    DefaultComboBoxModel<String> dcbm = new DefaultComboBoxModel<String>();
                    for (int i = 0; i < cb.getModel().getSize(); ++i) {
                        dcbm.addElement(cb.getModel().getElementAt(i));
                    }
                    m_cb.setModel(dcbm);
                    System.out.println("Set Internal.m_cb's model with " + m_cb.getModel().getSize() + " elements");
                }
            });
            super.add(m_cb);
        }
    }

    private JFrame frame;
    private JComboBox<String> jce = new JComboBox<String>();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SSCE window = new SSCE();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public SSCE() {
        initialize();
    }

    private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        jce.setSelectedIndex(0); // <-- Seems to have undefined behavior?
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Internal internal = new Internal(jce);
        jce.setModel(new DefaultComboBoxModel<String>());
        frame.getContentPane().add(internal, BorderLayout.NORTH);
        frame.getContentPane().add(jce, BorderLayout.CENTER);

        JButton btnAddRandomItem = new JButton("Break");
        btnAddRandomItem.addActionListener(new AbstractAction() {
            private static final long serialVersionUID = 1L;
            private int i;

            public void actionPerformed(ActionEvent e) {
                try {
                    ((DefaultComboBoxModel<String>) jce.getModel())
                            .addElement(String.valueOf(i++));
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    ObjectOutputStream out = new ObjectOutputStream(baos);
                    out.writeObject(SSCE.this);
                    ByteArrayInputStream in = new ByteArrayInputStream(baos
                            .toByteArray());
                    ObjectInputStream oin = new ObjectInputStream(in);
                    oin.readObject();
                    System.out.println("After returning, Internal.m_cb's model size is " + internal.m_cb.getModel().getSize() + " elements");
                    // The line below updates 'Internal' as expected, uncomment
                    // to see...
                    // however with the listener fired from the serialization
                    // method, it breaks?
                    // jce.setSelectedIndex(0);
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        });
        frame.getContentPane().add(btnAddRandomItem, BorderLayout.SOUTH);
    }

}

Set Internal.m_cb's model with 1 elements
Set Internal.m_cb's model with 1 elements
After returning, Internal.m_cb's model size is 1 elements
Set Internal.m_cb's model with 2 elements
After returning, Internal.m_cb's model size is 1 elements
Set Internal.m_cb's model with 3 elements
After returning, Internal.m_cb's model size is 1 elements
Set Internal.m_cb's model with 4 elements
After returning, Internal.m_cb's model size is 1 elements
Set Internal.m_cb's model with 5 elements
After returning, Internal.m_cb's model size is 1 elements

1 个答案:

答案 0 :(得分:1)

我认为您没有更新您认为正在更新的内容。如果附加到每个println m_cb.hashCode(),则输出相同。请注意,下面的“Set ...”错误消息表示每次都在m_cb的不同实例上设置模型。

Set Internal.m_cb's model with 1 elements: 44160343
Set Internal.m_cb's model with 1 elements: 1436306574
After returning, Internal.m_cb's model size is 1 elements: 44160343
Set Internal.m_cb's model with 2 elements: 2094948113
After returning, Internal.m_cb's model size is 1 elements: 44160343
Set Internal.m_cb's model with 3 elements: 915510800
After returning, Internal.m_cb's model size is 1 elements: 44160343
Set Internal.m_cb's model with 4 elements: 853795873
After returning, Internal.m_cb's model size is 1 elements: 44160343
Set Internal.m_cb's model with 5 elements: 616759228
After returning, Internal.m_cb's model size is 1 elements: 44160343
Set Internal.m_cb's model with 6 elements: 1385385632
After returning, Internal.m_cb's model size is 1 elements: 44160343
Set Internal.m_cb's model with 7 elements: 1283006722
After returning, Internal.m_cb's model size is 1 elements: 44160343

在每个序列化中创建了一个新的m_cb实例,并且您正在设置其模型 - 但这些新的组合框不会显示在您的框架中。