JTabbedPane更改侦听器以从文件读取

时间:2019-01-03 20:19:19

标签: java jtabbedpane

我有一个包含7个标签的JTabbedPane 每个选项卡都包含一个扩展JPanel的类, 我需要的是,如果我在任何选项卡中进行更改并保存到文件中,所做的更改都可以在其他选项卡中使用,而无需关闭程序并再次运行

我将标签添加到了JTabbedPane

tp.addTab("   Etudiant   ", new Etudiant());
            tp.addTab("   Enseignant   ", new Enseignant());
            tp.addTab("   Cours   ", new Cours());
            tp.addTab("   Groupes   ", new Groupe());
            tp.addTab("   Inscription   ", new Inscription());
            tp.addTab("   Horaires   ", new Horaires());
            tp.addTab("   Resultats   ", new Resultats());
            tp.addTab("   Divers   ", new Divers());

然后,我创建了一个更改监听器,每次选择一个标签时都从文件中读取内容, 我需要使用从每个选项卡的类中的文件读取的ArrayList

tp.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                switch (tp.getSelectedIndex()) {
                    case 3:

                        if (file3.length() > 0) {
                            try {
                                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Cours.out"));
                                listCours = (ArrayList) ois.readObject();

                                System.out.println("cours read");
                                System.out.println(listCours);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            listCours = new ArrayList();
                        }
                        if (file4.length() > 0) {
                            try {
                                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Inscriptions.out"));
                                listIns = (ArrayList) ois.readObject();

                                System.out.println("insc read");
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            listIns = new ArrayList();
                        }

                        break;

                    case 4:

                        if (file.length() > 0) {
                            try {
                                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Etudiants.out"));
                                listEtud = (ArrayList) ois.readObject();

                                System.out.println("etud read");
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            listEtud = new ArrayList();
                        }
                        if (file2.length() > 0) {
                            try {
                                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Groupes.out"));
                                listGroupes = (ArrayList) ois.readObject();

                                System.out.println("group read");
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            listGroupes = new ArrayList();
                        }
                        if (file3.length() > 0) {
                            try {
                                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Cours.out"));
                                listCours = (ArrayList) ois.readObject();

                                System.out.println("cours read");
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            listCours = new ArrayList();
                        }
                        break;
                    case 5:
                        break;

                    case 6:

                        break;

                    default:
                        break;
                }
            }
        });

1 个答案:

答案 0 :(得分:1)

ChangeListener的责任不是决定选择一个标签时应该做什么。相反,您需要某种方式来指示选定的选项卡,使其在被选择时应执行所需的任何操作。这样可以使代码脱钩,并允许您在不影响其余代码的情况下更改选项卡的顺序。

首先定义一个可加载对象的简单概念...

public interface Loadable {
    public void load();
}

然后,当选项卡选择更改时,要通知您的每个选项卡/面板都应实施此interface

public class LoadableTabPane extends JPanel implements Loadabel {
    //...
    public void load() {
        // Performing the loading here
    }
}

然后,当选项卡选择更改时,您检查选定的组件以确定它是instanceof Loadable,如果是,则将其强制转换并调用其load方法...

tp.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        Component selectedComponent = tp.getSelectedComponent();
        if (selectedComponent instanceof Loadable) {
            ((Loadable)selectedComponent).load();
        }
    }
}