I have some classes which are parameterized via their constructor. I need to provide a graphical user interface that lets the user specify these parameters. The only thing I can assume about the classes is that there is a constructor (default or with arguments).
[Update] I have three sets of classes Aimpl, Bimpl, Cimpl. The classes in set Aimple extend class A, classes in Bimpl extend class B, classes in Cimpl extend class C. Instances of A, B and C are used to compute on data. At compile time I don't know which subclasses are available, so I scan for implementations and add them to the appropriate set. If the end user wants to apply a computation he needs to instantiate it. To instantiate it, he needs to provide proper arguments to the constructor. The end user is a programmer himself and knows of arrays, abstract classes and interfaces. He even knows what reasonable arguments are.
At first only primitive types (or classes that expect primitive types) were expected, so I wrote a solution myself that worked fine. But now I have to deal with arrays, abstract classes and interfaces. So I'm looking for a library that could simplify the task of providing input fields to the user, extracting the input and creating objects.
If there is no such library, how would I do this myself? For the arrays I could use JTexFields and then parse the input, for abstract classes and interfaces I could scan for extending/implenting classes (using the reflections library) and provide a JComboBox to let the user select. I am new to programming GUIs so I can't reliably estimate if my approach is feasible and I don't want to waste time solving a problem that was solved before.
If information is missing, please feel free to point it out.
SWoeste is correct reflection is something intended for tasks similar to yours, but it is not very simple to use.
In fact, your question is not about swing (or GUI at all). You are solving the problem "How to create Object from String." (or it can be at least reduced to it). If your user is a programmer as you wrote I think that very simple solution for this is to use JSON (my opinion is that it uses very simple and understandable format), my code is for gson 1.6.
If I have an object, let say FullTimeWorker, that extends Person:
package net.betlista.gson;
public class Person {
private String name;
private String surname; // no setter
public String getName() {
System.out.println( "Person#getName()" );
return name;
}
public void setName( final String name ) {
this.name = name;
}
}
package net.betlista.gson;
public class FullTimeWorker extends Person {
private double salary;
}
Now you can create FullTimeWorker class as
package net.betlista.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class DeserializationTest {
public static void main( final String[] args ) {
final GsonBuilder gb = new GsonBuilder();
final Gson gson = gb.create();
// {
// "salary": 1234.5,
// "name": "John",
// "surname": "Doe",
// }
final String in = "{\"salary\":1234.5,\"name\":\"John\",\"surname\":\"Doe\"}";
final FullTimeWorker w = gson.fromJson( in, FullTimeWorker.class );
}
}
All you need now in GUI is JTextField (or better JTextArea) to insert JSON string, JComboBox for class selection and JButton, so simple.
It's clear that your GUI can be almost perfect - it depends only on your imagination, but I'm not sure if there is something simpler than that ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With