mardi 13 octobre 2015

Reflection: create an instance -java

I'm trying to build a dynamic gui.

I have the following class:

Public abstract  class Property
String ip1;
int port;

And a lot of sub class created from this class, for example the following two:

public   class PropertyDns extends Property
 string ip2;
 int port2;

Public class PropretyNetBios extends Property
long time;

The goal: The user chooses one of the sub properties and then i want to Present the fields he need to fill in order to create the instance. For example: if he is chosen PropertyDns I Will Present: "ip1:____ port1:______ ip2:_______ port2:______" When he will finish, he will press "ok" and then i want to create an instance of this class with the value he chose.

is there any way it can be done?

i Managed to Present the fields using reflection:

    Field[] s1=p.getClass().getSuperclass().getDeclaredFields();  
            Field[] s=p.getClass().getDeclaredFields();
for (int i = 0; i < s.length; i++) {
    ans[i]=s[i].getName();
}

but i think is Poorly done.





Aucun commentaire:

Enregistrer un commentaire