我在 Vaadin 中作業,我想檢索組件中選擇的所有值,但它們始終為空。
這是定義組件并將其存盤在 ArrayLIst 中以發送到專案的另一個類和方法的類。
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,并單擊 Stats。我做錯了什么?我需要使用哪種方法來獲得選擇。非常感謝。
我正在按要求添加實作的 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);
}
}
uj5u.com熱心網友回復:
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!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/484562.html
上一篇:<form>動作不是相對的
下一篇:按多個條件分組串列,LINQ
