vaadin返回组件值

发布于 2025-02-03 06:35:43 字数 3053 浏览 3 评论 0原文

我以瓦丁的形式工作,我想检索组件中选择的所有值,但它们总是空的。

这是类是将组件定义并存储在阵列列表中以发送到项目的其他类和方法。

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

此提交按钮将此配置写入全球POJO类。

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

而且,同一实例,如果我将其发送到此类外部的新方法,我总是会得到一个空值。

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

所有这些组件都有信息。选择了所有ComboBox,并单击统计数据。 我在做什么错?我需要使用哪种方法来获取选择。 非常感谢。


我正在按要求添加实现的ComboBoxs类。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情丝乱 2025-02-10 06:35:43

您的fieldgetter正在查看错误的位置。这些divs没有设置文本(这可能是您认为会通过combobox.setValue(CustomValue)进行的。

通常,您的代码有点令人费解。您不需要所有这些divs和抽象。

尝试

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.vaadin.com的启动器),选择“快速”,然后删除“快速”,输入“慢”,然后命中&lt; return/enter/enter&gt;。然后单击“打印”按钮。

输出看起来像这样:

值从null更改为快速
价值从快速变为null
价值从空变为慢
[快速,实时,慢]
现在的价值是:慢

希望这会有所帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文