Jannik Vöhl
Jannik Vöhl

Reputation: 9

Filling the Combobox with choices

I'm just a complete beginner when it comes to programming or java. So for the start my plan was to create a window useing JavaFX(combined with scene builder) where I do have a button that leads me to another window where i do have a combobox. I googled for hours now to find a way to fill that combobox with choices but all the solutions i found don't work for me. Thats why I think I made some mistakes here and I hope you can somehow help me. Or at list give me a hint what I should learn/read to get to the solution myself. So to start with, here's my main.java code where I build my first stage.

main.java:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root= FXMLLoader.load(getClass().getResource("Scene-Hauptmenu.fxml"));
            primaryStage.setTitle("Fishbase");
            primaryStage.sizeToScene();
            primaryStage.setResizable(false);
            primaryStage.setScene(new Scene(root));
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }       
    }

    public static void main(String[] args) {
        launch(args);
    }   
}

In my "Scene-Hauptmenu.fxml" all that matters is the button that leads me to my second window:

Scene-Hauptmenu.fxml:

<Button id="btn_gefangen" fx:id="btn_gefangen" mnemonicParsing="false" onAction="#gefangen" text="Ich habe Fische gefangen!" GridPane.rowIndex="1" />

So far everything works fine and I can switch to my second window without a problem. But I think my main problem lies within my controller class so here it is.

MyController.java:

public class MyController implements Initializable{
    private Node node;
    private Stage stage;
    private Scene scene;
    private FXMLLoader fxmlLoader;
    private Parent root;

    @FXML
    private Button btn_gefangen;

    @FXML
    private ComboBox<String> chobo_fisch; 

    @FXML
    private Button btn_gefangen_zurueck;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void gefangen(ActionEvent event) throws IOException{

        node = (Node) event.getSource();
        stage = (Stage) node.getScene().getWindow();
        scene = stage.getScene();

        fxmlLoader = new FXMLLoader (getClass().getResource("gefangen.fxml"));

        root = (Parent) fxmlLoader.load();
        scene.setRoot(root);
        stage.sizeToScene();
        stage.setTitle("Fische eintragen");          
    }


    public void gefangen_zurueck(ActionEvent event) throws IOException{
        node = (Node) event.getSource();
        stage = (Stage) node.getScene().getWindow();
        scene = stage.getScene();
        fxmlLoader = new FXMLLoader (getClass().getResource("Scene-Hauptmenu.fxml"));
        root = (Parent) fxmlLoader.load();
        scene.setRoot(root);
        stage.sizeToScene();
        stage.setTitle("Fishbase");     
    }   
}

So the button "btn_gefangen" leads me to that other window where i do have the combobox with the fx:id "chobo_fisch".

gefangen.fxml:

<ComboBox fx:id="chobo_Fisch" prefWidth="150.0"/>

So I googled for hours but I still didnt find any solution to fill the combobox with choices that works with my code. What did I do wrong? Can anyone help me here?

Best regards

Jannik

Upvotes: 0

Views: 676

Answers (4)

Nexonus
Nexonus

Reputation: 816

I found three variants, depending on your setup:

1st variant

// Weekdays 
String week_days[] = 
    { "Monday", "Tuesday", "Wednesday", 
      "Thrusday", "Friday" }; 

// Create a combo box 
ComboBox combo_box = new ComboBox(FXCollections.observableArrayList(week_days)); 

(Soure: https://www.geeksforgeeks.org/javafx-combobox-with-examples/)

2nd variant

final ComboBox emailComboBox = new ComboBox();
emailComboBox.getItems().addAll(
            "[email protected]",
            "[email protected]",
            "[email protected]",
            "[email protected]",
            "[email protected]"  
        );

Source: (https://docs.oracle.com/javafx/2/ui_controls/combo-box.htm)

3rd variant (for FXML)

<ComboBox fx:id="someName">
     <items>
         <FXCollections fx:factory="observableArrayList">
              <String fx:value="1"/>
              <String fx:value="2"/>
              <String fx:value="3"/>
              <String fx:value="4"/>
          </FXCollections>
      </items>
      <value>
           <String fx:value="1"/>
      </value>
</ComboBox>

Edit

As mentioned by fabian you should make sure to include the FXML imports:

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

The second one may not be needed.

Upvotes: 3

Hypnosus
Hypnosus

Reputation: 11

I'm new to those stuff but I think this is how it should look or at least close too if I understood what you wanted. Example below:

ComboBox<String> stuff = new ComboBox<>(); stuff.getItems().addAll("1","2","5","10");

Note: I'm new to stackoverflow.

Upvotes: 1

Vincent Passau
Vincent Passau

Reputation: 822

Your combobox must be filled with items (in your case String):

    List<String> list = new ArrayList<String>();
    list.add("Item 1");
    list.add("Item 2");
    chobo_fisch.setItems(FXCollections.observableArrayList(list));

If you use a combobox of a more complex object, you could use a cellfactory to choose the value that is displayed :

    chobo_fisch.setCellFactory(obj -> new ChoboFischListCell());
    chobo_fisch.setButtonCell(new ChoboFischListCell());

where ChoboFischListCell is a class that extends ListCell and where you implement which field of your object should be displayed.

Upvotes: 0

JeSa
JeSa

Reputation: 607

Try this:

ObservableList<String> items = FXCollections.observableArrayList();
    items.add("a");
    items.add("b");
    chobo_fisch.getItems().addAll(items);

Upvotes: 0

Related Questions