How to make SimpleComboBox display my default value

I have a cycle that builds our questionnaires. I have a function that I call strings the correct type. Here is the section that creates the combo box:

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

I want to set the default response at the prompt so I can check it later. The problem is that this is not setting the selected value in my combo box. What am I missing?

+4
source share
2 answers

Assuming you have an "answer". You can get its index from List<String> choices .

 int answerIndex = choices.indexOf(answer); simpleComboBox.select(answerIndex); 

Or you can directly use simpleComboBox.select(answer); in case of String

If you want to show the default text, you can use

 simpleComboBox.setEmptyText("Select an answer...."); 
+2
source

you can do this using your working code below

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

All Articles