Example usage for javafx.scene.control ComboBox setCellFactory

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

Introduction

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

Prototype

public final void setCellFactory(Callback<ListView<T>, ListCell<T>> value) 

Source Link

Usage

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/*from www .  j a  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) {
    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 a2 s  . com
        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(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//  ww  w  .ja v  a  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();
}

From source file:Main.java

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

    ComboBox emailComboBox = new ComboBox();
    emailComboBox.getItems().addAll("A", "B", "C", "D", "E");
    emailComboBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override//from  ww  w . ja v  a2 s  . co  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;
        }

    });

    GridPane grid = new GridPane();
    grid.setVgap(4);
    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:de.bayern.gdi.gui.Controller.java

private void loadWfsSimple() {
    ObservableList<Node> children = simpleWFSContainer.getChildren();
    Map<String, String> parameters = downloadConfig.getParams();
    for (Node node : children) {
        if (node instanceof HBox) {
            HBox hb = (HBox) node;/*from  ww w  .jav  a2  s  .c  om*/
            Node n1 = hb.getChildren().get(0);
            Node n2 = hb.getChildren().get(1);
            if (n1 instanceof Label && n2 instanceof TextField) {
                Label paramLabel = (Label) n1;
                TextField paramBox = (TextField) n2;
                String targetValue = parameters.get(paramLabel.getText());
                if (targetValue != null) {
                    paramBox.setText(targetValue);
                }
            }
            if (n2 instanceof ComboBox) {
                ComboBox<OutputFormatModel> cb = (ComboBox<OutputFormatModel>) n2;
                cb.setCellFactory(new Callback<ListView<OutputFormatModel>, ListCell<OutputFormatModel>>() {
                    @Override
                    public ListCell<OutputFormatModel> call(ListView<OutputFormatModel> list) {
                        return new CellTypes.StringCell();
                    }
                });
                cb.setOnAction(event -> {
                    if (cb.getValue().isAvailable()) {
                        cb.setStyle(FX_BORDER_COLOR_NULL);
                    } else {
                        cb.setStyle(FX_BORDER_COLOR_RED);
                    }
                });
                boolean formatAvailable = false;
                for (OutputFormatModel i : cb.getItems()) {
                    if (i.getItem().equals(downloadConfig.getOutputFormat())) {
                        cb.getSelectionModel().select(i);
                        formatAvailable = true;
                    }
                }
                if (!formatAvailable) {
                    String format = downloadConfig.getOutputFormat();
                    OutputFormatModel m = new OutputFormatModel();
                    m.setItem(format);
                    m.setAvailable(false);
                    cb.getItems().add(m);
                    cb.getSelectionModel().select(m);
                }
                if (cb.getValue().isAvailable()) {
                    cb.setStyle(FX_BORDER_COLOR_NULL);
                } else {
                    cb.setStyle(FX_BORDER_COLOR_RED);
                }
            }
        }
    }
}