0

I would like to make a program where I can add something like "plug-ins". Lets say I have Project1 where I have JFrame and Project2 where I have JPanel.

The point is, that user can develop his own programs (plug-ins) and than add it to the main program (Project1).

So... I export frame as runnable JAR file and export panel as runnable JAR file (or only as JAR file?).

What I want to do is to show panel in frame.

Simply I want to do something like this or this but only in Java and with panel, I don't need to run any .exe programs. (It's going to run on Linux not Windows).

My professional picture that should describe what I want to do :) image

Is there some way to do this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
KoRaP CZ
  • 53
  • 6

1 Answers1

0

For the plugin part, you can look at this project that I created, the loadPlugin method loads plugins from jar files.

And for the JPanel part, you just have to let to plugins a way (an API surely) that lets them change the displayed panel of the JFrame (with JFrame::setContentPane). I hope that'll help you, ask me for more precision if you need.

Edit:

As an example you can get the JPanel from the .jar and instantiate it like this :

URLClassLoader classLoader = new URLClassLoader(new URL[]{file.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
String packageName = "package.where.is.the.jpanel.to.load";
JPanel jpanel = (JPanel) Class.forName(packageName, true, classLoader).getConstructor().newInstance();
Paragoumba
  • 88
  • 1
  • 10
  • Thank you for Answer. I dont exactly know how to make that JPanel. I cant find any JFrame::setContentView method. But maybe JFrame::getContentPanel is the same? Lets say that I add plugin to my server and I need to show JPanel in my JFrame? Can you show me some code how to do that? – KoRaP CZ Nov 03 '18 at 17:26
  • Oops sorry it's [setContentPane](https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#setContentPane(java.awt.Container)) – Paragoumba Nov 03 '18 at 18:37
  • But I know the instance of JFrame, because that is my main program. I run JFrame and then JFrame runs .jar file (from another folder) where is JPanel. So I need to get instance of JPanel and do something like JPanel jPanel = instance. But I dont need to call some methods from JPanel or something, just need to put it in some area (like in that video that I linked), so user cant move it or something... And when user click button, JPanel will show up or hide. – KoRaP CZ Nov 03 '18 at 20:22
  • I edited my answer with an example that shows you how to instantiate a custom JPanel from a plugin – Paragoumba Nov 04 '18 at 13:44