سؤال

I have a JTabbedPane like the one in this picture:

My Tabbed Pane

I have a class for each tab (HouseGUI, CSPGUI, VPPGUI, and many others). Each class has a method called writeToXML()

I need to call the writeToXML() method of each "class" in my JTabbedPane when I press the "Save All" button. But I don't really know how to do it. Can you help me?

Here's what I've done so far:

    if (e.getSource() == saveAllButton) {
        int totalTabs = tabbedPane.getTabCount();
        ArrayList<ArrayList<String>> salvationForAll = new ArrayList<>();
        ArrayList<Method> methods = new ArrayList<>();
        Method[] array = new Method[50];
        for (int i = 0; i < totalTabs; i++) {
            try {
                String title = tabbedPane.getTitleAt(i);
                String tmp = title;
                tmp = tmp.replaceAll("\\s", "");
                array = Class.forName("tabbedpaneInterfaces."+ tmp +"GUI").getMethods();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddComponents.class.getName()).log(Level.SEVERE, null, ex);
            }

            methods = convertToArrayList(array);
            int methodSize = methods.size();
            for (int j = 0; j < methodSize; j++) {
                //TO DO call WriteToXML()                       
            }                   
        }               
    }

How can I call the methods I need, in runtime?

هل كانت مفيدة؟

المحلول

Make all you *UI classes implements the same interface :

public interface XMLWritable {
  void writeToXml(); 
}

public class HouseGUI implements XMLWritable {
   public void writeToXml() {
     //XML writing stuff
   }

}

----
for (int i = 0; i < totalTabs; i++) {
 if(tabbedPane.getComponentAt(i) instanceof XMLWritable ) {
   ((XMLWritable) tabbedPane.getComponentAt(i)).writeToXml();
 }
}

To conclude, it is not very maintainable to mix UI and persistence stuffs, but it is not the purpose of your question.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top