0

I have a special problem in coding a dynamic menu in Java.

What I want: I want a menubar that looks for special files (example: addon01_men.class) and adds the content of this file to the menu as a new item.

Problem: To invoke the methode/class in the file, I've to invoke it in the main file. But the addon-class is needed in this case, because java checks the presence of the methode/class. So the program will not start, if "addon01_men.class" is missing.

My solutions didn't work (perhaps in my fault): - Reflections - Override

I don't want to use dynamic classes because it's necessary to compile when the program starts - because not all PCs that can run java, can compile java.

I'd be pleased when someone has a working idea. Thank you.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    [`FileMenu`](http://stackoverflow.com/a/4039359/230513) may be of interest. – trashgod Aug 23 '12 at 19:55
  • yes. thats interesting. but not that is not what I search. Thank you for help. I try to explain more closely: The main-program has a JMenu. In the File "addon01_men.class" is a additional menupoint: `class addon01_men extends JMenuItem implements ActionListener { public addon01_men () { super ("Add01"); addActionListener (this); } public void actionPerformed(ActionEvent e) { new addon01_pl(); } }` I need a solution to invoke "addon01_men", if "addon01_men.class" is in the folder, but do not, when its not (the NOT is the problem) – Marcel Krammer Aug 23 '12 at 20:40
  • You can use `Action` to encapsulate your reflective code. Please edit your question, where new code will be more readable. – trashgod Aug 23 '12 at 23:41

1 Answers1

1

You should start simple like this example using Reflections:

http://www.mkyong.com/java/how-to-use-reflection-to-call-java-method-at-runtime/ For want you want, you need look for a directory containing the class files like addon01_men.class (think like they're plugins), load them and create instances.

Pedro Nunes
  • 410
  • 2
  • 5
  • That helps me a lot, thank you. I tried a lot with this and it seems it could work nearly. Problem is with this I can invoke methods. So I tried to make methods to add(addon01_men); ... but this don't work. I need to invoke classes/constructor... I found something after that on [link]http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html[/link]. But thats a little bit freaky. – Marcel Krammer Aug 23 '12 at 22:05
  • @MarcelKrammer Reflection in Java is ugly. That is what you need, though, for the question you asked. Either use [`Class.newInstance()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29) to call a no-argument constructor, or [`Class.getConstructor()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getConstructor%28java.lang.Class...%29) to get a Constructor object on which you can call [`.newInstance()`](http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html#newInstance%28java.lang.Object...%29) with arguments. – dsh Aug 23 '12 at 22:35
  • @dsh thank you. Sorry for the many bones you guys have to throw me, but I think I only need a line, that has an effect like "add(new obj)"... if obj is an instance of a constructor... found nothing in JavaDoc... – Marcel Krammer Aug 23 '12 at 23:04