Home > Mobile >  Vaadin returning the component values
Vaadin returning the component values

Time:06-02

I am working in a form in Vaadin, and I want to retrieve all the values selected in the components, but they are always empty.

This is the class were the components are defined and stored in a ArrayLIst to send to another classes and methods of the project.

public class MainView extends Div {

    ProjectConfiguration configuration = new ProjectConfiguration();

    private final List<String> visualizationList = new ArrayList<>(Arrays.asList("FAST", "REAL TIME"));
    private final List<String> resolutionList = new ArrayList<>(Arrays.asList("320 x 200", "640 x 400"));
    private final List<String> audioRate = new ArrayList<>(Arrays.asList("44100", "48000"));

    public void ShowForm() {

        ComboBoxs comboResolution = new ComboBoxs(resolutionList,"Video resolution","Select a resolution for the exported video");
        ComboBoxs comboVideoVisualization = new ComboBoxs(visualizationList,"Export visualization", "Select the visualization of the video export");
        ComboBoxs comboAudioRate = new ComboBoxs(audioRate,"Audio rate", "Select the audio rate for the video export");
        CheckStats checkStats = new CheckStats();
        ArrayList<Div> fieldValues = new ArrayList<Div>();
        fieldValues.add(comboResolution);
        fieldValues.add(comboVideoVisualization);
        fieldValues.add(comboAudioRate);
        fieldValues.add(checkStats);
        SubmitButton submitButton = new SubmitButton(configuration, fieldValues);
    }
}

And this Submit button is writting this configuration into a global POJO class.

public class SubmitButton extends Div {

public SubmitButton(ProjectConfiguration configuration, ArrayList<Div> fields) {
    Button submitButton = new Button("Create video");

    submitButton.addClickListener(clickEvent -> {
        submitConfiguration(configuration, fields);
    });
    add(submitButton);
}
private void submitConfiguration(ProjectConfiguration configuration, ArrayList<Div> fields) {
    new FieldGetter(configuration, fields);
}
}

And this same instance, if I send it to a new method outside to this class I always get an empty value.

public class FieldGetter {

    public FieldGetter(ProjectConfiguration configuration, ArrayList<Div> fields, String exportFileName) {
        log.info("Text 1 "   fields.get(0).getText());
        log.info("Text 2 "   fields.get(1).getText());
        log.info("Text 3 "   fields.get(2).getText());
        log.info("Text 4 "   fields.get(3).getText());
}}

All these components have information. All ComboBox is selected, and the Stats is clicked. What I'm doing wrong? Which method I need to use to get the selection. Thank you very much.


I'm adding the ComboBoxs class implemented, as requested.

@Route("combo")
public class ComboBoxs extends Div {

    public ComboBoxs(List<String> arrayList, String label, String helperText) {
        ComboBox<String> comboBox = new ComboBox<>(label);
        comboBox.setAllowCustomValue(true);
        comboBox.addCustomValueSetListener(e -> {
            String customValue = e.getDetail();
            arrayList.add(customValue);
            comboBox.setItems(arrayList);
            comboBox.setValue(customValue);
        });
        add(comboBox);
        comboBox.setItems(arrayList);
        comboBox.setHelperText(helperText);
    }
}

CodePudding user response:

Your FieldGetter is looking at the wrong place. Those Divs have no texts set (which might be what you assumed would happen by comboBox.setValue(customValue)).

Generally, your code is a bit convoluted. You don't need all those Divs and abstractions.

Try

public class HelloWorldView extends HorizontalLayout {

private final List<String> visualizationList = new ArrayList<>(Arrays.asList("FAST", "REAL TIME"));

public HelloWorldView() {
    ComboBox<String> comboBox = new ComboBox<>("Visualisation");
    comboBox.setAllowCustomValue(true);
    comboBox.addCustomValueSetListener(e -> {
        String customValue = e.getDetail();
        visualizationList.add(customValue);
        comboBox.setItems(visualizationList);
        comboBox.setValue(customValue);
    });
    add(comboBox);
    comboBox.setItems(visualizationList);

    Button printValues = new Button("Print");
    printValues.addClickListener(e -> {
        System.out.println(Arrays.toString(visualizationList.toArray(new String[0])));
        System.out.println("Value is now:"  comboBox.getValue() );
    });
    comboBox.addValueChangeListener(e-> System.out.println("Value changed from "    e.getOldValue()   " to "   e.getValue()));
    add(printValues);        }       }

Start, the program (I used the starter from start.vaadin.com), select "FAST", then delete "FAST", enter "Slow" and hit <return/enter>. Then click the print button.

The output will look like this:

Value changed from null to FAST
Value changed from FAST to null
Value changed from null to Slow
[FAST, REAL TIME, Slow]
Value is now:Slow

Hope this helps!

  • Related