I would like to load a java class dynamically, and then add objects created with this class in an ObservableList<myLoadedClass>
for example.
Class<?> thisClass = Class.forName("Point", true, classLoader);
Object iClass = thisClass.newInstance();
...
ObservableList<thisClass> data = FXCollections.observableArrayList();
The last line cause error
Cannot find symbol: class thisClass...
Thank you for your help.
Ok for the wildcard but then ...
Class<?> thisClass = Class.forName("Point", true, classLoader);
Object iClass = thisClass.newInstance();
...
ObservableList<?> data = FXCollections.observableArrayList();
data.addAll(iClass);
Last line cause: No suitable method found for addAll(Object).
Any ideas?
Five (or a bit more ;-) minutes later...
Instead of wildcard, 'Object' made the trick. Inspired by This thread where they explain: "So a Collection of unknown type is not a collection that can take any type. It only takes the unknown type. And as we don't know that type (its unknown ;) ), we can never add a value, because no type in java is a subclass of the unknown type."
So, for now, my solution is:
Class<?> thisClass = Class.forName("Point", true, classLoader);
Object iClass = thisClass.newInstance();
...
ObservableList<Object> data = FXCollections.observableArrayList();
data.addAll(iClass);
// Sample Point class method
Method getXMethod = thisClass.getDeclaredMethod("getX");
System.out.println(getXMethod.invoke(data.get(0)));