0

I'm trying to add a plugin system where people can install their own custom looks and feels. I'm not sure how I'd actually install it so it could be found by going through UIManager.getInstalledLookAndFeels()?

I've tried this:

public static void registerPlugins()
{
    File[] files = pluginDir.listFiles();
    for(int i = 0; i < files.length; i++)
    {
        if(files[i].getName().endsWith(".config"))
        {
            System.out.println("Found plugin");
            File jarfile = new File(pluginDir,files[i].getName().substring(0,files[i].getName().length()-7)+".jar");
            if(jarfile.exists())
            {
                System.out.println("Found jar");
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new FileReader(files[i]));
                    String name = reader.readLine(); //Unused for now
                    String classname = reader.readLine();
                    reader.close();
                    reader = null;
                    URL url = jarfile.toURI().toURL();  
                    URL[] urls = new URL[]{url};

                    ClassLoader cl = new URLClassLoader(urls);

                    Class<?> lafClass = cl.loadClass(classname);
                    final LookAndFeel laf =  (LookAndFeel)lafClass.newInstance();

                    UIManager.getLookAndFeelDefaults().put("ClassLoader", cl);
                    UIManager.installLookAndFeel(laf.getName(), laf.getClass().getName());                  
                } catch (Exception e) {
                    try {
                        if(reader != null)
                        reader.close();
                    } catch (Exception es) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    e.printStackTrace();
                }

            }
        }
    }
}

This gets the config files from the plugins folder that have contents like Web look and feel com.alee.laf.WebLookAndFeel and loads the respective jar. So far, all it did was crash cause it could not load components, or list the look and feel while also not being able to find it, so like the list SHOWS "WebLookAndFeel" but trying to use com.alee.laf.WebLookAndFeel causes an exception.

Any advice?

Hello234
  • 175
  • 2
  • 12
  • I only managed to get one to load but i kept getting errors saying that the ui for a component was not found – Hello234 Feb 05 '19 at 00:10
  • You may do `Thread.currentThread().setContextClassLoader(…);` in the EDT, but obviously, only once, so this custom loader would have to load all pluggable look & feels you want to offer. Reference: [`UIManager.setLookAndFeel(String)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/UIManager.html#setLookAndFeel(java.lang.String)): “*Loads the LookAndFeel specified by the given class name, using the current thread's context class loader…*” – Holger Feb 15 '19 at 15:29

0 Answers0