Skip to content
Advertisement

Array to ComboBox – JavaFx

I am working on a school project, I am trying to bring an a String array straight into a combo box and am getting an error.

Error: java: incompatible types: java.lang.String[] cannot be converted to javafx.collections.ObservableList<java.lang.String>

ComboBox comboDecks;
ComboBox comboTrucks;
ComboBox comboWheels;
private final String[] deckArray = {"The Master Thrasher - $60 ", "The Dictator - $ 45", "The Street King - $ 50"};
private final String[] trucksArray = {"7.75-inch axle - $35 ", "8-inch axle - $40 ", "8.5-inch axle - $45 "};
private final String[] wheelsArray = {"51mm - $20", "55mm - $22", "58mm - $24", "61mm - $28"};


@Override
public void start(Stage stage) throws IOException {

    title1 = new Text("Skateboard Designer");
    title2 = new Text("Choose from the following Decks, Trucks, Wheels, and Accessories to design and price your own skateboard!");
    lblDecks = new Label("Decks: ");
    lblTrucks = new Label("Trucks: ");
    lblWheels = new Label("Wheels: ");
    lblAccess = new Label("Accessories: ");
    comboDecks = new ComboBox(deckArray);
    comboTrucks = new ComboBox(trucksArray);
    comboWheels = new ComboBox(wheelsArray);

Advertisement

Answer

That ComBoBox constructor needs an ObservableList as argument . if you want to add an array of Strings use addAll() method

comboDecks = new ComboBox();
comboDecks.getItems.addAll(deckArray)
comboTrucks = new ComboBox();
comboTrucks .getItems.addAll(trucksArray)
comboWheels = new ComboBox();
comboWheels.getItems.addAll(wheelsArray)
Advertisement