Java 如何让SimpleCompoBox显示我的默认值

Java 如何让SimpleCompoBox显示我的默认值,java,gxt,Java,Gxt,我有一个循环来建立我们的调查问卷。我有一个函数,我把构建称为正确的类型。以下是构建组合框的部分: Field<?> field = null; if (item instanceof MultipleChoiceQuestionDTO) { MultipleChoiceQuestionDTO multipleChoice = (MultipleChoiceQuestionDTO) item; SimpleComboBox<String> cmbQuestion =

我有一个循环来建立我们的调查问卷。我有一个函数,我把构建称为正确的类型。以下是构建组合框的部分:

Field<?> field = null;
if (item instanceof MultipleChoiceQuestionDTO) {
  MultipleChoiceQuestionDTO multipleChoice = (MultipleChoiceQuestionDTO) item;
  SimpleComboBox<String> cmbQuestion = new SimpleComboBox<String>();
  String prompt = multipleChoice.getPrompt();
  cmbQuestion.setFieldLabel(ImageViewer.formatPrompt(prompt));
  List<String> choices = new ArrayList<String>();
  choices.add(prompt);
  for (String choice : multipleChoice.getPossibleAnswers()) {
    choices.add(choice);
  }
  cmbQuestion.add(choices);
  cmbQuestion.setEditable(false);
  cmbQuestion.setForceSelection(true);
  cmbQuestion.setSimpleValue(prompt);
  field = cmbQuestion;
}
Field=null;
如果(多个EchoiceQuestiondTo的项目实例){
多回音问题到多回音问题=(多回音问题到)项;
SimpleCompoBox cmbQuestion=新的SimpleCompoBox();
String prompt=multipleChoice.getPrompt();
cmbQuestion.setFieldLabel(ImageViewer.formatPrompt(prompt));
列表选项=新建ArrayList();
选项。添加(提示);
for(字符串选择:multipleChoice.getPossibleAnswers()){
选择。添加(选择);
}
问题。添加(选项);
cmbQuestion.setEditable(false);
CMB问题。setForceSelection(真);
cmbQuestion.setSimpleValue(提示);
字段=问题;
}
我想将默认答案设置为提示符,以便稍后进行测试。问题是,这并没有在我的组合框上设置所选的值。我遗漏了什么?

假设你有一个“答案”。您可以从
列表选项中获取它的索引

int answerIndex = choices.indexOf(answer);
simpleComboBox.select(answerIndex);
也可以直接使用
simplembobox,选择(答案)如果是
字符串

如果要显示默认文本,则可以使用

simpleComboBox.setEmptyText("Select an answer....");

您可以使用下面的代码来完成这项工作

String answer = simpleComboBox.getValue().toString(); //or default value
simpleComboBox.setSimpleValue(answer);

你能详细说明一下吗?是否要在SimpleCompoBox中选择“选择”列表中默认选中的一项?或者您希望显示一些默认文本,提示用户选择其中一个有效选项?两者都有。如果已经有一个选择,我想选择该值。如果没有,我想显示默认文本并强制选择。选择的执行方式与上面的一样。它选择该项,但不会在组合框中显示为条目。我真的不明白为什么这不起作用。:(除了调用
select()
,你能试试
setValue(answer)
。理想情况下它应该起作用,但我看到有时需要调用
setValue
。例如-
simplemboBox.select(answerIndex);simplembobox.setValue(answer);