How to iterate through a JavaFX-combobox?


Question

My question is simple: How do I iterate throught a javafx-combobox? I tried so much and nothing works. The foreach-construct does not work and combobox does not even have methods for size and single object retrieval, eliminating the possibility to use a simple-for-loop. Am I missing something?

1
0
7/2/2013 1:38:27 PM

Accepted Answer

Yes you are missing..or maybe not. But, nevertheless, just return ObservableList of objects from your ComboBox and use for each to iterate through that collection. Here is an example:

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class ComboBoxExample extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        GridPane gridPane = new GridPane();
        Button button = new Button("Print items from ComboBox");
        final ComboBox<String> comboBox = new ComboBox<String>();
        //Add some items
        comboBox.getItems().addAll("John","Jane","Laina");

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                //To iterate through ComboBox
                ObservableList<String> items = comboBox.getItems();
                for(String item : items){
                    System.out.println(item.toString());
                }
            }
        });

        gridPane.add(comboBox,0,0);
        gridPane.add(button,0,1);

        primaryStage.setScene(new Scene(gridPane,320,280));
        primaryStage.show();
    }
}
3
7/2/2013 1:58:21 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon