Example usage for javafx.scene.control ComboBox getItems

List of usage examples for javafx.scene.control ComboBox getItems

Introduction

In this page you can find the example usage for javafx.scene.control ComboBox getItems.

Prototype

public final ObservableList<T> getItems() 

Source Link

Usage

From source file:cz.lbenda.rcp.DialogHelper.java

/** Open dialog with chooser when user can choose single option
 * @param question question which is show to user
 * @param items list of items which user can choose
 * @param <T> type of item/*from   w  ww.j  a va2s.  c  o  m*/
 * @return null if user click on cancel or don't choose anything, elsewhere choosed item */
@SuppressWarnings("unchecked")
public static <T> T chooseSingOption(String question, T... items) {
    if (items.length == 0) {
        return null;
    }
    Dialog<T> dialog = new Dialog<>();
    dialog.setResizable(false);
    dialog.setTitle(chooseSingleOption_title);
    dialog.setHeaderText(question);

    ComboBox<T> comboBox = new ComboBox<>();
    comboBox.getItems().addAll(items);
    dialog.getDialogPane().setContent(comboBox);

    ButtonType btCancel = ButtonType.CANCEL;
    ButtonType btOk = ButtonType.OK;
    dialog.getDialogPane().getButtonTypes().addAll(btCancel, btOk);

    Optional<T> result = dialog.showAndWait();
    if (result.isPresent()) {
        if (btCancel == result.get()) {
            return null;
        }
        if (btOk == result.get()) {
            return comboBox.getSelectionModel().getSelectedItem();
        }
    } else {
        return null;
    }
    return result.get();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);
    ComboBox<String> myComboBox = new ComboBox<String>();
    myComboBox.getItems().addAll("A", "B", "C", "D", "E");
    myComboBox.setEditable(true);/*w w w  .j  av  a  2 s .  com*/
    myComboBox.setPromptText("Email address");

    Group root = (Group) scene.getRoot();
    root.getChildren().add(myComboBox);
    stage.setScene(scene);
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);
    ComboBox<String> myComboBox = new ComboBox<String>();
    myComboBox.getItems().addAll("A", "B", "C", "D", "E");
    myComboBox.setValue("A");
    System.out.println(myComboBox.getValue());

    Group root = (Group) scene.getRoot();
    root.getChildren().add(myComboBox);/*from   ww  w.  j  av a2  s . c  o  m*/
    stage.setScene(scene);
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);

    ComboBox<String> myComboBox = new ComboBox<String>();
    myComboBox.getItems().addAll("A", "B", "C", "D", "E");
    myComboBox.setEditable(true);/*from   w w w.  j av a2s  .  co m*/

    Group root = (Group) scene.getRoot();
    root.getChildren().add(myComboBox);
    stage.setScene(scene);
    stage.show();

}

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);
    ComboBox<String> myComboBox = new ComboBox<String>();
    myComboBox.getItems().addAll("A", "B", "C", "D", "E");
    myComboBox.setValue("A");
    System.out.println(myComboBox.getValue());

    myComboBox.setValue(null);// w  w w.jav  a  2  s .c o  m

    Group root = (Group) scene.getRoot();
    root.getChildren().add(myComboBox);
    stage.setScene(scene);
    stage.show();
}

From source file:Main.java

@Override
public void start(final Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 400, 300, Color.WHITE);

    GridPane gridpane = new GridPane();

    ComboBox<Rectangle> cmb = new ComboBox<Rectangle>();
    cmb.getItems().addAll(new Rectangle(10, 10, Color.RED), new Rectangle(10, 10, Color.GREEN),
            new Rectangle(10, 10, Color.BLUE));

    gridpane.add(cmb, 2, 0);//from w  w  w .j a  va 2  s .c om

    root.getChildren().add(gridpane);
    primaryStage.setScene(scene);

    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);

    ComboBox<String> emailComboBox = new ComboBox<String>();
    emailComboBox.getItems().addAll("A", "B", "C", "D", "E");
    emailComboBox.setValue("A");
    System.out.println(emailComboBox.getValue());

    GridPane grid = new GridPane();
    grid.setVgap(4);/*from ww w .  j  a va 2  s  . c  o  m*/
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(new Label("To: "), 0, 0);
    grid.add(emailComboBox, 1, 0);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();

}

From source file:Main.java

@Override
public void start(final Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 400, 300, Color.WHITE);
    GridPane gridpane = new GridPane();
    ComboBox<Color> cmb = new ComboBox<Color>();
    cmb.getItems().addAll(Color.RED, Color.GREEN, Color.BLUE);
    cmb.setCellFactory(new Callback<ListView<Color>, ListCell<Color>>() {
        @Override/*from   w  w w .j  a v  a  2 s. c om*/
        public ListCell<Color> call(ListView<Color> p) {
            return new ListCell<Color>() {
                private final Rectangle rectangle;
                {
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                    rectangle = new Rectangle(10, 10);
                }

                @Override
                protected void updateItem(Color item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item == null || empty) {
                        setGraphic(null);
                    } else {
                        rectangle.setFill(item);
                        setGraphic(rectangle);
                    }
                }
            };
        }
    });

    gridpane.add(cmb, 2, 0);

    root.getChildren().add(gridpane);
    primaryStage.setScene(scene);

    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 200, 200);
    ComboBox<String> myComboBox = new ComboBox<String>();
    myComboBox.getItems().addAll("A", "B", "C", "D", "E");
    myComboBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override//w  w  w .ja v a 2 s . c  o  m
        public ListCell<String> call(ListView<String> param) {
            final ListCell<String> cell = new ListCell<String>() {
                {
                    super.setPrefWidth(100);
                }

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (item != null) {
                        setText(item);
                        if (item.contains("A")) {
                            setTextFill(Color.RED);
                        } else if (item.contains("B")) {
                            setTextFill(Color.GREEN);
                        } else {
                            setTextFill(Color.BLACK);
                        }
                    } else {
                        setText(null);
                    }
                }
            };
            return cell;
        }
    });
    Group root = (Group) scene.getRoot();
    root.getChildren().add(myComboBox);
    stage.setScene(scene);
    stage.show();
}

From source file:Main.java

@Override
public void start(final Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 400, 300, Color.WHITE);

    GridPane gridpane = new GridPane();

    ComboBox<Color> cmb = new ComboBox<Color>();
    cmb.getItems().addAll(Color.RED, Color.GREEN, Color.BLUE);

    cmb.setCellFactory(new Callback<ListView<Color>, ListCell<Color>>() {
        @Override//  w w w  .  j  a  va  2 s  .  c  o  m
        public ListCell<Color> call(ListView<Color> p) {
            return new ListCell<Color>() {
                private final Rectangle rectangle;
                {
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                    rectangle = new Rectangle(10, 10);
                }

                @Override
                protected void updateItem(Color item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item == null || empty) {
                        setGraphic(null);
                    } else {
                        rectangle.setFill(item);
                        setGraphic(rectangle);
                    }
                }
            };
        }
    });

    gridpane.add(cmb, 2, 0);

    root.getChildren().add(gridpane);
    primaryStage.setScene(scene);

    primaryStage.show();
}