Implementing Objects Through a GUI in Java

I have some classes that are parameterized via their constructor. I need to provide a graphical user interface that allows the user to specify these options. The only thing I can assume about classes is that a constructor exists (either by default or with arguments).

[Update] I have three sets of classes Aimpl, Bimpl, Cimpl. Classes in the Aimple set extend class A, classes in Bimpl extend class B, classes in Cimpl extend class C. Instances A, B, and C are used for computation on data. At compile time, I don't know what subclasses are available, so I go through the implementations and add them to the appropriate set. If the end user wants to apply the calculation, he needs to create an instance. In order to create an instance, it must provide the correct arguments to the constructor. The end user is a programmer himself and knows arrays, abstract classes and interfaces. He even knows what reasonable arguments are.

At first, only primitive types were expected (or classes expecting primitive types), so I wrote a solution myself that worked great. But now I have to deal with arrays, abstract classes and interfaces. Therefore, I am looking for a library that will simplify the task of providing input fields to the user, retrieving input, and creating objects.

If there is no such library, how would I do it myself? For arrays, I could use JTexFields and then parse the input, for abstract classes and interfaces, which I could scan to extend / inject classes (using library reflections ) and provide a JComboBox for the user to choose from. I'm new to GUI programming, so I can't reliably assess if my approach is possible, and I don't want to waste time solving a problem that has been solved before.

If information is missing, do not hesitate to provide it.

+3


source to share


1 answer


SWoeste is the right reflection designed for tasks like yours, but it's not very easy to use.

Actually, your question is not about rocking (or GUI in general). You are solving the problem "How to create an object from a string". (or it can at least be reduced to it). If your user is a programmer, as you wrote, I think a very simple solution for this is to use JSON (I believe it uses a very simple and straightforward format), my code is for gson 1.6.

If I have an object, say FullTimeWorker, that extends Person:

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;
    }

}

      

FullTimeWorker



package net.betlista.gson;

public class FullTimeWorker extends Person {

    private double salary;

}

      

Now you can create the 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 );
    }

}

      

GUI

All you need now in the GUI is a JTextField (or better JTextArea) to insert a JSON string, a JComboBox to select a class and a JButton, so easy.

Clearly, your GUI could be nearly perfect - it just depends on your imagination, but I'm not sure if there is something simpler ;-)

+1


source







All Articles