swing компоненты ведут себя не так после десериализации
В JFrame (javax.swing) есть 2 панели JPanels. Мы говорим об одном из них. Эта JPanel называется " workspace". Пользователь добавляет туда перетаскиваемые компоненты, которые расширяют мой абстрактный класс "LogicComponent". Эти компоненты имеют объекты, которые расширяют JButton. Проблема заключается в сериализации и десериализации всех компонентов из " workpace".
Вот код сохранения/загрузки:
LButton Save = new LButton("Save");
Save.setBounds(140, 400, 100, 50);
Save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
FileOutputStream fos = new FileOutputStream(JOptionPane.showInputDialog(null, "Save as:", "Logic",
PLAIN_MESSAGE));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(workspace);
oos.close();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "<html>Something went wrong.<br/><html/>" + ex, "Logic",
JOptionPane.PLAIN_MESSAGE);
}
}
});
gates.add(Save);
LButton Load = new LButton("Load");
Load.setBounds(20, 400, 100, 50);
Load.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
FileInputStream fis = new FileInputStream(JOptionPane.showInputDialog(null, "Load:", "Logic",
PLAIN_MESSAGE));
ObjectInputStream ois = new ObjectInputStream(fis);
for(Component c : ((JPanel) ois.readObject()).getComponents())
workspace.add((LogicComponent) c);
frame.repaint();
ois.close();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "<html>Something went wrong.<br/><html/>" + ex, "Logic", JOptionPane.PLAIN_MESSAGE);
}
}
});
gates.add(Load);
Загруженные компоненты не перетаскиваются, а компоненты внутри компонентов не взаимодействуют с пользователем.
Я также пробовал это:
LButton Load = new LButton("Load");
Load.setBounds(20, 400, 100, 50);
Load.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
FileInputStream fis = new FileInputStream(JOptionPane.showInputDialog(null, "Load:", "Logic",
PLAIN_MESSAGE));
ObjectInputStream ois = new ObjectInputStream(fis);
frame.remove(workspace);
workspace = (JPanel) ois.readObject();
frame.add(workspace);
frame.repaint();
ois.close();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "<html>Something went wrong.<br/><html/>" + ex, "Logic", JOptionPane.PLAIN_MESSAGE);
}
}
});
gates.add(Load);
Тот же результат.
Или мне всё таки использовать javaFX?
Спасибо.