Reputation: 15
ComboBox
and CheckBox
set element's inside it https://ibb.co/7YWQfLh, but don't display this element's https://ibb.co/Mfy2mZb
I create boxes in different panes (HBox
, VBox
, AnchorPane
, GridPane
), result the same.
I used Enum and usual String and result the same.
public class TestFXController {
@FXML
private ComboBox<String> asd;
@FXML
private ChoiceBox<String> fgh;
@FXML
void initialize() {
ObservableList<String> langs =
FXCollections.observableArrayList("Java", "JavaScript", "C#", "Python");
asd = new ComboBox<>(langs);
fgh = new ChoiceBox<>(langs);
}
}
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TestFXController">
<children>
<ComboBox fx:id="asd" prefWidth="150.0" />
<ChoiceBox fx:id="fgh" layoutX="14.0" layoutY="70.0" prefWidth="150.0" />
</children>
</AnchorPane>
What's wrong. I want this result.
https://metanit.com/java/javafx/pics/4.16.png
Upvotes: 0
Views: 972
Reputation: 3187
Alternatively to what Robert said if you want less code in your project you can add the items into the fxml like so
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TestFXController">
<children>
<ComboBox fx:id="asd" prefWidth="150.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Java" />
<String fx:value="JavaScript" />
<String fx:value="C#" />
<String fx:value="Python" />
</FXCollections>
</items>
</ComboBox>
<ChoiceBox fx:id="fgh" layoutX="14.0" layoutY="70.0" prefWidth="150.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Java" />
<String fx:value="JavaScript" />
<String fx:value="C#" />
<String fx:value="Python" />
</FXCollections>
</items>
</ChoiceBox>
</children>
</AnchorPane>
Upvotes: 1
Reputation: 470
Your Combo box and Choice box already exists as long as they have to correct fx:ids set in your FXML (asd, and fgh), so you don't need the following:
asd = new ComboBox<>(langs);
fgh = new ChoiceBox<>(langs);
From your initialize method you can for example set your items in the combo box like this:
ObservableList<String> langs = FXCollections.observableArrayList("Java", "JavaScript", "C#", "Python");
asd.setItems(langs);
That should work fine, it does for me as you can see in the image below, as long as you've got the correct fx:id set (which you look to have).
Hope it helps :)
Upvotes: 2