Julie
Julie

Reputation: 1

Changing values to combobox from an array

I need to load values from an array to a combobox, with this code it loads only the last value of the array? Can anyone help me please.

for(int i =0; i<lines.size(); i++) {
    resultArray[i] = lines.get(i).split("\t");

    Laptops[i] = resultArray[i][0];
    ObservableList<String> option = FXCollections.observableArrayList(Laptops[i].toString());

    cbx1.setValue("");
    cbx1.setItems(option);

    cbx2.setValue("");
    cbx2.setItems(option);

    cbx3.setValue("");
    cbx3.setItems(option);
}

Upvotes: 0

Views: 69

Answers (1)

Zephyr
Zephyr

Reputation: 10263

In your loop, you are creating a brand new List on each iteration. So when you call setItems() on your ComboBox, the option list only has that one item in it.

There are several other issues with your loop, but once you have a valid array, populating a ComboBox with it is quite simple using the Arrays.asList() method:

ObservableList<String> option = FXCollections.observableList(Arrays.asList(resultArray));
cbx1.setItems(option);

That being said, I doubt you're getting a proper array with your line:

resultArray[i] = lines.get(i).split("\t");

Without knowing what lines actually is in your code, it's difficult to address this specifically, but it looks like you could skip the whole resultArray altogether and just use lines.

Calling the split() method returns an array of Strings anyway. Perhaps you could remove the loop altogether and just try:

ObservableList<String> options = 
    FXCollections.observableArrayList(Arrays.asList(lines.split("\t"));

Upvotes: 3

Related Questions