-1

I have a JTabbedPane containing 7 tabs each tab contains a class extending JPanel, what I need is if I change in any tab and save to a file, the changes made could be used in the other tabs without closing the program and running it again

I added the tabs to the 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());

then I created a change listener to read from a file each time I choose a tab, I need to use the ArrayList read from the file in the class(of each tab)

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;
                }
            }
        });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

1

It's not the responsibility of the ChangeListener to be making decisions about what should be done when a tab is selected. Instead, you need some way to instruct the selected tab that it should perform what ever operations it needs to when selected. This decouples the code and allows you to change the order of the tabs without affect the remaining code.

Start by defining a simple concept of something which is loadable...

public interface Loadable {
    public void load();
}

Then each tab/panel you want to be notified when the tab selection changes should implement this interface

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

Then when the tab selection changes, you inspect the selected component to determine of it's an instanceof Loadable and if it is, you cast it and call its load method...

tp.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        Component selectedComponent = tp.getSelectedComponent();
        if (selectedComponent instanceof Loadable) {
            ((Loadable)selectedComponent).load();
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366