Reputation: 1537
Using jdk1.7.0_80.jdk:
I have 2 ComboBoxes; I want to select a value from the mainComboBox and subCombobox gets loaded automatically based on the value selected from the mainComboBox. and with its unique set of choices in the subComboBox: My initialization works as seen in the screenshot below.
Now my issue is when I change the value of the mainComboBox. when I do that nothing gets preselected/displayed in the subComoboBox. I am using comboBoxChoice to load the different options available in the ObservableList. But for some odd reason it's not kicking in.
public class FXMLDocumentController implements Initializable {
ObservableList<String> mainList = FXCollections.observableArrayList("Main-1","Main-2","Main-3");
ObservableList<String> main1SubList = FXCollections.observableArrayList("Main1-Sub1","Main1-Sub2","Main1-Sub3");
ObservableList<String> main2SubList = FXCollections.observableArrayList("Main2-Sub1","Main2-Sub2","Main2-Sub3");
ObservableList<String> main3SubList = FXCollections.observableArrayList("Main3-Sub1","Main3-Sub2","Main3-Sub3");
@FXML
private ComboBox mainComboBox, subComboBox;
@Override
public void initialize(URL url, ResourceBundle rb) {
mainComboBox.setValue("Main-1");
mainComboBox.setItems(mainList);
subComboBox.setValue("Main1-Sub1");
subComboBox.setItems(main1SubList);
}
@FXML
private void comboBoxChoice() {
if (mainComboBox.getValue().equals("Main-1") ) {
subComboBox.setValue("Main1-Sub1");
subComboBox.setItems(main1SubList);
}
if (mainComboBox.getValue().equals("Main-2") ) {
subComboBox.setValue("Main2-Sub1");
subComboBox.setItems(main2SubList);
}
if (mainComboBox.getValue().equals("Main-3") ) {
subComboBox.setValue("Main3-Sub1");
subComboBox.setItems(main3SubList);
}
}
}
FXML file:
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication18.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<ComboBox fx:id="mainComboBox" layoutX="103.0" layoutY="136.0" onAction="#comboBoxChoice" prefWidth="150.0" />
<ComboBox fx:id="subComboBox" layoutX="103.0" layoutY="162.0" prefWidth="150.0" />
</children>
</AnchorPane>
Upvotes: 0
Views: 70
Reputation: 209724
This works fine for me on JDK 1.8.0_66. It may be that this is an old bug that has since been fixed.
I would recommend switching the order of setItems()
and setValue()
; it's likely that replacing all the items removes the old selected item, even if it is in the "new" list.
I.e.
@FXML
private void comboBoxChoice() {
if (mainComboBox.getValue().equals("Main-1") ) {
subComboBox.setItems(main1SubList);
subComboBox.setValue("Main1-Sub1");
}
if (mainComboBox.getValue().equals("Main-2") ) {
subComboBox.setItems(main2SubList);
subComboBox.setValue("Main2-Sub1");
}
if (mainComboBox.getValue().equals("Main-3") ) {
subComboBox.setItems(main3SubList);
subComboBox.setValue("Main3-Sub1");
}
}
Upvotes: 1