为每个comboBox项设置值

时间:2016-08-20 07:12:17

标签: codenameone

填充组合框代码:

<!DOCTYPE html>
<html>
<body onload="playGame()">
<p>Welcome to my color guessing game</p>
<script language = "JavaScript">
function inArray(needle, haystack){
    for (var i = 0; i <haystack.length(); i++){
        if (needle === haystack[i]){
            return true;
        }
    }
    return false;
}
function changeBackground(color){
    document.body.style.background = color;
}
function playGame(){
    var correct = false; 
    alert("Correct status is: " + correct);
    var colorArray = ["cyan", "gold", "green", "gray", "magenta", "blue", "red", "orange", "yellow", "white"];
    alert("test1");
    for (var i = 0; i < colorArray.length(); i++){
        alert("for " + i + " the color is " + colorArray[i]);
    }
    colorArray = colorArray.sort();
    alert("answer index is " + answerIndex);
    alert("color array length is " + colorArray.length());
    var answerIndex = Math.floor(Math.random()*colorArray.length());
    alert("the resulting color from the color array is: " + answerColor);
    var answerColor = colorArray[answerIndex];
    alert("The correct color is " + answerColor);
    var answerList = colorArray.join(", ");
    var guessCount = 0;
    while(!correct){
        var colorGuess = prompt("Welcome to my guessing game! The colors available for your choosing are: " + "\n\n" + answerList + "\n\n" + "Which color am I thinking of?");
        guessCount++;
        if (!inArray(colorGuess, colorArray)){
            alert("Your guess wasn't one of the selections that was available or I otherwise don't recognize it." + "\n\n" + "Please try again!");
        }
        else{
            if (colorArray.indexOf(colorGuess)<color.indexOf(answerColor)){
                alert("Your guess was alphabetically before the correct color! Try again.");

            }
            else if (colorARray.indexOf(colorguess)>color.indexOf(answerColor)){
                alert("Your guess was alphabetically after the correct color! Try again.");
            }
            else{
                alert("Your guess is correct!");
                correct = true;
                changeBackground(answerColor);
            }
        }
    }
    alert("Great job!" + "\n\n" + "You took " + guessCount + " guesses to get the correct answer!");
}
</script>
</body>
</html>

categoryId取自上面的for循环,如何在每个组合框中设置它?我需要获取每个选定的组合框项目的categoryId,我怎么能得到它?

1 个答案:

答案 0 :(得分:0)

你有几种方法可以做到这一点。

一种方法就是:

getCategoryComboBox.addItem(entry);

这将为您提供getSelectedItem()有效解决该问题的完整条目。

要使名称正确呈现,您需要执行此操作:

cb.setRenderer(new DefaultListCellRenderer<Object>() {
   public Component getCellRendererComponent(Component list, Object model, Object value, int index, boolean isSelected) {
      if(value instanceof Map) {
         value = ((Map)value).get("name");
      }
      return super.getCellRendererComponent(list, model, value, index, isSelected);
   }
});

请注意,您还需要将主题常量otherPopupRendererBool定义为false才能使其正常工作。