0

I have an old swing Jcombobox which i used to use like this.

baudRatecomboBox = new JComboBox(baudRates);
    baudRatecomboBox.setSelectedIndex(1);
    GridBagConstraints gbc_baudRateComboBox = new GridBagConstraints();
    gbc_baudRateComboBox.fill = GridBagConstraints.HORIZONTAL;
    gbc_baudRateComboBox.insets = new Insets(0, 0, 5, 5);
    gbc_baudRateComboBox.gridx = 1;
    gbc_baudRateComboBox.gridy = 3;
    getContentPane().add(baudRatecomboBox, gbc_baudRateComboBox);
String[] baudRates = { "2400", "4800", "9600", "14400", "19200", "38400", "56000", "115200"  };

I am re-writing my application using JavaFX and i cant get the comboBox to populate.

This is my FXML

<ComboBox id="baudRatecomboBox" fx:id="baudRateComboBox" prefHeight="30.0" prefWidth="87.0" promptText="Baud" />

and this is my Java

@FXML
ComboBox baudRateComboBox;

public void start(Stage primaryStage) {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("LaserControllerUI.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setTitle("Laser Controller");
        primaryStage.setScene(scene);
        primaryStage.show();
        scene.getStylesheets().add
         (LaserControllerUI.class.getResource("LaserControllerUI.css").toExternalForm());
        ComboBox<String> baudRateComboBox = new ComboBox();
        baudRateComboBox.getItems().addAll(baudRates);       
        baudRateComboBox.setVisible(true);

Why isnt my ComboBox populating?

Display Name
  • 405
  • 6
  • 26
  • 1
    You have to use an ´ObservableList´ with JavaFX. Take a look at how [Comboboxes](http://docs.oracle.com/javafx/2/ui_controls/combo-box.htm) should be filled – Nico T Aug 09 '16 at 10:18
  • 1
    @NicoT A `ObservableList` is used: The one returned by `ComboBox.getItems()`, which is also possible. – fabian Aug 09 '16 at 11:04

1 Answers1

2

You're simply not adding the rates to the ComboBox that is shown on screen.

Since you seem to let FXMLLoader create the controller instance, you'll also won't get the ComboBox injected to the Application object that belongs to the start method.

BTW: In most cases it's a bad idea to use the Application class as controller.

In case the set of items in the ComboBox is always the same, an initialize method of the controller can be used to set the items. Furthermore it's always a good idea to specify the type parameters.

@FXML
private ComboBox<String> baudRateComboBox;

@FXML
private void initialize() {
    // called by FXMLLoader after injecting fields
    baudRateComboBox.getItems().addAll(baudRates);
}

You could also specify the items directly in the fxml file:

<ComboBox id="baudRatecomboBox" fx:id="baudRateComboBox" prefHeight="30.0" prefWidth="87.0" promptText="Baud">
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="2400"/> 
            <String fx:value="4800"/> 
            <String fx:value="9600"/> 
            <String fx:value="14400"/> 
            <String fx:value="19200"/> 
            <String fx:value="38400"/> 
            <String fx:value="56000"/> 
            <String fx:value="115200"/>
        </FXCollections>
    </items>
</ComboBox>

This requires the following imports as processing instructions:

<?import java.lang.String?>
<?import javafx.collections.FXCollections?>

If you need to pass the baudRates array to the controller instead, you can find some solutions here: Passing Parameters JavaFX FXML

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Hi, Thanks for your detailed response. I now have it working. Could you explain what 'ComboBox baudRateComboBox' does in my code then? it come from a tutorial and i thought that was initialising the combobox? – Display Name Aug 10 '16 at 06:18
  • 1
    @DisplayName The information you've posted isn't enough to be sure, but I recommend reading the description at the end of the following example http://stackoverflow.com/documentation/javafx/1580/fxml-and-controllers/5125/example-fxml#t=201608100629012634808 which describes how the loading process works. Probably the `baudRateComboBox` field of *some* instance is set by the `FXMLLoader`, but even if it was the field of the instance the `start` method is called for, you're never using the field, but use a local variable instead. – fabian Aug 10 '16 at 06:36