Search code examples
javaserviceloader

Service loader in java loading modules but can display loaded views properly


So my application has a main project that loads all classes that implement IModule. Each Module has a view that extends AbstractView. Here is the sample code

public abstract class AbstractView extends JPanel{
    //Init panel
}
public Module implements IModule{
   public AbstractView getView(){
       return some class that extends AbstractView;
   }
   public String getModuleName(){
       return "ModuleName";
   }
}

public Application{
   ServiceLoader<IModules> modules; //Loaded at start of application
   public void displayModule(String moduleName){
       Iterator<IModule> moduleIterator = modules.iterator();
       while (moduleIterator.hasNext()) {
            if (module.getModuleName() == moduleName) {
               display(module.getView());
            }
   }
   public void display(AbstractView view){
      displayPanel.add(view, Borderlayout.Center);
      displayPanel.repaint();
   }
}

Its just simplified example but thats pretty much how it works. My problem is I cant display the plug in view. Im not sure why that doesnt work. When I print the loaded View I get the class that extended that AbstractView with its properties like type of layout etc. The classes that implemented interfaces that had no relation to UI seems to have no issues atm. I load plug ins from a folder that contains their respective jar files.

EDIT

In my META-INF/services I only have app.IModule file that contains the text plugin.Plugin. The plug in class is the class that implemented IModule. That is the only service I declared since it is the class that the service loader will load. Do I have to declare the class that extends AbstractView as well? Even without loading it in the service loader


Solution

  • this is kinda embarassing. I spent two hours working on this when I only forgot the displayPanel.validate();.... sorry about that