Search code examples
javavaadinvaadin7vaadin8

Random Element as input and get the Value


I don't know if it's a duplicated Question cause I've found nothing and actually I didn't know what Keywords should I've been searching.

I want to have a class which gets an Element as input and then shows the Value of that Element.

for Example:

public void showValue(Object obj){
    System.out.printLn("output: " + obj.getValue());
}

and then:

NativeSelect ns=new NativeSelect();
TextField tf=new TextField();

ns.addValue("Name");
ns.select("Name");

tf.setValue("LastName");

showValue(ns);
showValue(tf);

and have this output:

output: Name
output: LastName

could maybe someone help me or give me an idea how should i do that! I'm new to Java and started programming after a long time.

Thanks alot!


Solution

  • You want a function that can print the value of every field. Like this:

    public static void showValue(Field f) {
       System.out.println("output: "f.getValue()); //Will print it via console
       new Notification("output: "+f.getValue()).show(Page.getCurrent()); 
       //Will show a text box in your current page
    }
    

    As from Field docs (link), every field has a getValue(). All that you should take care is that value types that you use in your fields should have overriden toString so this method doesn't show the default toString.