下面列出了javafx.scene.control.ComboBox#setButtonCell ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ComboBox<ConstellationColor> makeNamedCombo() {
final ObservableList<ConstellationColor> namedColors = FXCollections.observableArrayList();
for (final ConstellationColor c : ConstellationColor.NAMED_COLOR_LIST) {
namedColors.add(c);
}
final ComboBox<ConstellationColor> namedCombo = new ComboBox<>(namedColors);
namedCombo.setValue(ConstellationColor.WHITE);
final Callback<ListView<ConstellationColor>, ListCell<ConstellationColor>> cellFactory = (final ListView<ConstellationColor> p) -> new ListCell<ConstellationColor>() {
@Override
protected void updateItem(final ConstellationColor item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
final Rectangle r = new Rectangle(12, 12, item.getJavaFXColor());
r.setStroke(Color.BLACK);
setText(item.getName());
setGraphic(r);
}
}
};
namedCombo.setCellFactory(cellFactory);
namedCombo.setButtonCell(cellFactory.call(null));
return namedCombo;
}
/**
* Prompts the user to select a terminal from a provided list.
*
* @param terminals a list of terminals for the user to choose from
* @return the user's selected terminal
*/
private Optional<UtilityTerminal> promptForTerminalSelection(List<UtilityTerminal> terminals) {
// create a dialog for terminal selection
ChoiceDialog<UtilityTerminal> utilityTerminalSelectionDialog = new ChoiceDialog<>(terminals.get(0), terminals);
utilityTerminalSelectionDialog.initOwner(mapView.getScene().getWindow());
utilityTerminalSelectionDialog.setTitle("Choose Utility Terminal");
utilityTerminalSelectionDialog.setHeaderText("Junction selected. Choose the Utility Terminal to add as the trace element:");
// override the list cell in the dialog's combo box to show the terminal name
@SuppressWarnings("unchecked") ComboBox<UtilityTerminal> comboBox =
(ComboBox<UtilityTerminal>) ((GridPane) utilityTerminalSelectionDialog.getDialogPane()
.getContent()).getChildren().get(1);
comboBox.setCellFactory(param -> new UtilityTerminalListCell());
comboBox.setButtonCell(new UtilityTerminalListCell());
// show the terminal selection dialog and capture the user selection
return utilityTerminalSelectionDialog.showAndWait();
}
private void initComboBox(ComboBox<Integer> combo, final String[] values) {
combo.setCellFactory((param) -> new OptionListCell(values));
combo.setButtonCell(new OptionListCell(values));
for (int i = 0; i < values.length; i++) {
combo.getItems().add(i);
}
}
private void initComboBox(ComboBox<Integer> combo, final String[] values) {
combo.setCellFactory((param) -> new OptionListCell(values));
combo.setButtonCell(new OptionListCell(values));
for (int i = 0; i < values.length; i++) {
combo.getItems().add(i);
}
}
private Tuple2<ComboBox<PriceFeedComboBoxItem>, VBox> getMarketPriceBox() {
VBox marketPriceBox = new VBox();
marketPriceBox.setAlignment(Pos.CENTER_LEFT);
ComboBox<PriceFeedComboBoxItem> priceComboBox = new JFXComboBox<>();
priceComboBox.setVisibleRowCount(12);
priceComboBox.setFocusTraversable(false);
priceComboBox.setId("price-feed-combo");
priceComboBox.setPadding(new Insets(0, -4, -4, 0));
priceComboBox.setCellFactory(p -> getPriceFeedComboBoxListCell());
ListCell<PriceFeedComboBoxItem> buttonCell = getPriceFeedComboBoxListCell();
buttonCell.setId("price-feed-combo");
priceComboBox.setButtonCell(buttonCell);
Label marketPriceLabel = new Label();
updateMarketPriceLabel(marketPriceLabel);
marketPriceLabel.getStyleClass().add("nav-balance-label");
marketPriceLabel.setPadding(new Insets(-2, 0, 4, 9));
marketPriceBox.getChildren().addAll(priceComboBox, marketPriceLabel);
model.getMarketPriceUpdated().addListener((observable, oldValue, newValue) ->
updateMarketPriceLabel(marketPriceLabel));
return new Tuple2<>(priceComboBox, marketPriceBox);
}
default <T> void initComboBox(final ComboBox<T> box, final Map<T, Image> map, final T[] values) {
final ComboBoxFactoryList<T> factory = new ComboBoxFactoryList<>(map);
box.getItems().addAll(values);
box.setButtonCell(factory.call(null));
box.setCellFactory(factory);
}