0

I know the headline must be not clear enough, I do not know how else to right it in short, so let me explain myself.

I'm doing a java project, where I need to make some form to a class in gui.

Let's say I have the Student class.

Student.java

public class Student {
    int studendID;
    String studentName;

// and so on...
}

I've heard there is a way to make a gui form of any class i want, so that way the user its self can write the parameters of the class.

In that case it will auto make 2 labels with "studendID" and "studentName" and 2 rooms for text to import the user's choice.

And in case i would like to enter other class, i wouldn't need to build up again all the form to match another class, but it will do it automatic.

I've heard that it is possible, and i have no idea how.

i've tried looking through google, and i found nothing even similiar to it.

teobais
  • 2,820
  • 1
  • 24
  • 36
liranahum
  • 143
  • 3
  • 13
  • *I've heard there is a way to make a gui form of any class I want*. Sure. Make it. *but it will do it automatic*. No. No it won't. Ask whomever you heard that from. – Elliott Frisch Dec 31 '15 at 13:10
  • there's a way in making a class that can do that. i just don't even know how to start – liranahum Dec 31 '15 at 13:19
  • we used an IDE called [BlueJ](http://www.bluej.org/) at school, which allowed the programmer to manually create objects and edit its parameters .. are you talking about this? – jboockmann Dec 31 '15 at 13:27
  • Try a proper IDE instead of using Notepad. Theres nothing wrong with using Notepad but using an IDE as a beginner will help you drastically as it will help you with errors, automation, debug your code and many other helpful things. I would suggest using [Netbeans](https://netbeans.org/features/index.html). – Eric G Dec 31 '15 at 13:34
  • I think that what you are looking for is the concept of reflection, [What is reflection and why is it useful?](http://stackoverflow.com/q/37628/4207875), the accepted answer talk about methods, but you can inspect properties aswell, although, be ready for some heavy coding. I'd ponder if it is worth the effort before going all in. – saljuama Dec 31 '15 at 13:37

2 Answers2

1

You may use Java Reflection to get fields of a class, and then, make a UI by iterating these fields and let user set the value for each one.

See this other related question: What is reflection and why is it useful?

Community
  • 1
  • 1
Prim
  • 2,880
  • 2
  • 15
  • 29
  • 1
    not so fast, the question refeers to **any** class, what if one of the fields is another class? a simple loop iterating will not be enough. oh and btw, nice copy paste of my comment :) – saljuama Dec 31 '15 at 13:46
  • This talks about invoking methods inside an unknown classm what i wanted was to get to the data members's type inorder to make the view. sorry for not being clear enough. thank you anyway though. – liranahum Dec 31 '15 at 13:49
  • @SalvadorJuanMartinez I was writting my answer (and was looking for a related link) when you posted your comment, it's not a copy/paste of your answer ;) But we found the same link and idea, true. In any case, the example of its source code doesn't show any subclass, and i applied the KISS and YAGNI principles – Prim Dec 31 '15 at 13:53
  • @liranahum Reflection is about invoking methods, it's true, but also getting declared fields, attributes, annotations and so on. It's what you use in the accepted answer ;) It's perfect if you have found a solution of your problem – Prim Dec 31 '15 at 13:56
  • @Prim fair enough :) its just that it was almost identical – saljuama Dec 31 '15 at 15:43
1

You can create a loop in JSP through your fields in the instantiate class,

For example in java :

TestClass test = new TestClass();
        Field[] fs = test.getClass().getDeclaredFields();
        for (Field field : fs)
        {
            field.setAccessible(true);
            System.out.println( field.getName() + " " + field.get(test) );
        }

And create a input Text for each, hope it help

alias_boubou
  • 206
  • 6
  • 11