Java Code Examples for javafx.scene.control.ComboBox#setValue()

The following examples show how to use javafx.scene.control.ComboBox#setValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColorInputPane.java    From constellation with Apache License 2.0 8 votes vote down vote up
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;
}
 
Example 2
Source File: Properties.java    From CircuitSim with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Node createGui(Stage stage, T value, Consumer<T> onAction) {
	ComboBox<String> valueList = new ComboBox<>();
	
	for(T t : validValues) {
		valueList.getItems().add(toString.apply(t));
	}
	
	valueList.setValue(toString(value));
	valueList.getSelectionModel()
	         .selectedItemProperty()
	         .addListener((observable, oldValue, newValue) -> {
		         if(!newValue.equals(oldValue)) {
			         try {
				         onAction.accept(parse(newValue));
			         } catch(Exception exc) {
				         exc.printStackTrace();
			         }
		         }
	         });
	
	return valueList;
}
 
Example 3
Source File: GuiUtils.java    From kafka-message-tool with MIT License 5 votes vote down vote up
public static <T> void resetComboboxValue(ComboBox<T> comboBox, T value) {
    // if value (reference to object) does not change as combobox value
    // but the internals of reference  will change (eg. some field of object)
    // combobox will not reinitialize it , need to to clear and reset it manually

    if (comboBox.getValue() == value) {
        comboBox.setValue(null);
    }
    comboBox.setValue(value);
}
 
Example 4
Source File: EnumComboBox.java    From strongbox with Apache License 2.0 5 votes vote down vote up
public static <T extends Enum<T>> ComboBox<T> create(Class<T> enumType, T defaultEnumValue) {

        List<T> entries = new ArrayList<>();
        Collections.addAll(entries, enumType.getEnumConstants());

        ObservableList<T> observableList = FXCollections.observableList(entries);
        ComboBox<T> comboBox = new ComboBox<T>(observableList);
        comboBox.setValue(defaultEnumValue);

        return comboBox;
    }
 
Example 5
Source File: MetadataOverview.java    From sis with Apache License 2.0 5 votes vote down vote up
private ComboBox<String> createComboBox(final Map<String, Identification> m) {
    ComboBox<String> cb = new ComboBox<>();
    Collection<? extends Identification> ids = this.metadata.getIdentificationInfo();
    ObservableList<String> options = FXCollections.observableArrayList();
    int i = 1;
    if (ids.size() > 1) {
        for (Identification id : ids) {
            String currentName;
            if (id.getCitation() != null) {
                currentName = id.getCitation().getTitle().toString();
            } else {
                currentName = Integer.toString(i);
            }
            options.add(currentName);
            m.put(currentName, id);
        }
        cb.setItems(options);
        cb.setValue(ids.iterator().next().getCitation().getTitle().toString());
    } else if (ids.size() == 1) {
        if (ids.iterator().next().getCitation() != null) {
            m.put(ids.iterator().next().getCitation().getTitle().toString(), ids.iterator().next());
            cb.setValue(ids.iterator().next().getCitation().getTitle().toString());
            cb.setVisible(false);
        }
    } else {
        cb.setValue("No data to show");
        cb.setVisible(false);
    }
    return cb;
}
 
Example 6
Source File: DisplayEditor.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
private ToolBar createToolbar()
{
    final Button[] undo_redo = UndoButtons.createButtons(undo);

    final ComboBox<String> zoom_levels = new ComboBox<>();
    zoom_levels.getItems().addAll(JFXRepresentation.ZOOM_LEVELS);
    zoom_levels.setEditable(true);
    zoom_levels.setValue(JFXRepresentation.DEFAULT_ZOOM_LEVEL);
    zoom_levels.setTooltip(new Tooltip(Messages.SelectZoomLevel));
    zoom_levels.setPrefWidth(100.0);
    // For Ctrl-Wheel zoom gesture
    zoomListener zl = new zoomListener(zoom_levels);
    toolkit.setZoomListener(zl);
    zoom_levels.setOnAction(event ->
    {
        final String before = zoom_levels.getValue();
        if (before == null)
            return;
        final String actual = requestZoom(before);
        // Java 9 results in IndexOutOfBoundException
        // when combo is updated within the action handler,
        // so defer to another UI tick
        Platform.runLater(() ->
            zoom_levels.setValue(actual));
    });

    final MenuButton order = new MenuButton(null, null,
        createMenuItem(ActionDescription.TO_BACK),
        createMenuItem(ActionDescription.MOVE_UP),
        createMenuItem(ActionDescription.MOVE_DOWN),
        createMenuItem(ActionDescription.TO_FRONT));
    order.setTooltip(new Tooltip(Messages.Order));

    final MenuButton align = new MenuButton(null, null,
        createMenuItem(ActionDescription.ALIGN_LEFT),
        createMenuItem(ActionDescription.ALIGN_CENTER),
        createMenuItem(ActionDescription.ALIGN_RIGHT),
        createMenuItem(ActionDescription.ALIGN_TOP),
        createMenuItem(ActionDescription.ALIGN_MIDDLE),
        createMenuItem(ActionDescription.ALIGN_BOTTOM),
        createMenuItem(ActionDescription.ALIGN_GRID));
    align.setTooltip(new Tooltip(Messages.Align));

    final MenuButton size = new MenuButton(null, null,
        createMenuItem(ActionDescription.MATCH_WIDTH),
        createMenuItem(ActionDescription.MATCH_HEIGHT));
    size.setTooltip(new Tooltip(Messages.Size));

    final MenuButton dist = new MenuButton(null, null,
        createMenuItem(ActionDescription.DIST_HORIZ),
        createMenuItem(ActionDescription.DIST_VERT));
    dist.setTooltip(new Tooltip(Messages.Distribute));

    // Use the first item as the icon for the drop-down...
    try
    {
        order.setGraphic(ImageCache.getImageView(ActionDescription.TO_BACK.getIconResourcePath()));
        align.setGraphic(ImageCache.getImageView(ActionDescription.ALIGN_LEFT.getIconResourcePath()));
        size.setGraphic(ImageCache.getImageView(ActionDescription.MATCH_WIDTH.getIconResourcePath()));
        dist.setGraphic(ImageCache.getImageView(ActionDescription.DIST_HORIZ.getIconResourcePath()));
    }
    catch (Exception ex)
    {
        logger.log(Level.WARNING, "Cannot load icon", ex);
    }


    return new ToolBar(
        grid = createToggleButton(ActionDescription.ENABLE_GRID),
        snap = createToggleButton(ActionDescription.ENABLE_SNAP),
        coords = createToggleButton(ActionDescription.ENABLE_COORDS),
        new Separator(),
        order,
        align,
        size,
        dist,
        new Separator(),
        undo_redo[0],
        undo_redo[1],
        new Separator(),
        zoom_levels);
}
 
Example 7
Source File: GetGroupIdentifier.java    From strongbox with Apache License 2.0 4 votes vote down vote up
public Optional<SecretsGroupIdentifier> getGroupIdentifier() {
    Stage dialog = new Stage();
    dialog.initModality(Modality.WINDOW_MODAL);
    dialog.initOwner(parent);
    dialog.initStyle(StageStyle.UTILITY);

    VBox layout = new VBox();
    Label label = new Label("Create Secrets Group");

    Text nameLabel = new Text("Name:");
    TextField name = new TextField();
    HBox n = new HBox();
    n.getChildren().addAll(nameLabel, name);

    ObservableList<Region> regions =
            FXCollections.observableArrayList(Region.values());

    Text regionLabel = new Text("Region:");
    ComboBox<Region> region = new ComboBox<>(regions);
    region.setValue(defaultRegion);


    HBox r = new HBox();
    r.getChildren().addAll(regionLabel, region);

    HBox actions = new HBox();
    Button create = new Button("Create");
    Button cancel = new Button("Cancel");
    actions.getChildren().addAll(create, cancel);

    create.setOnAction(f -> {
        if (name.getText() != null) {
            secretsGroupIdentifier = Optional.of(new SecretsGroupIdentifier(region.getValue(), name.getText()));
        }
        dialog.close();
    });

    cancel.setOnAction(f -> {
        dialog.close();
    });

    layout.getChildren().addAll(label, n, r, actions);

    Scene scene2 = new Scene(layout, 370, 250);
    dialog.setScene(scene2);

    dialog.showAndWait();


    return secretsGroupIdentifier;
}
 
Example 8
Source File: ContainerEngineSettingsPanelSkin.java    From phoenicis with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Updates the engine settings in the given {@link GridPane engineSettingsGrid}
 *
 * @param engineSettingsGrid The GridPane containing the shown engine settings
 */
private void updateEngineSettingsGrid(final GridPane engineSettingsGrid) {
    engineSettingsGrid.getChildren().clear();

    final ContainerDTO container = getControl().getContainer();

    for (EngineSetting engineSetting : getControl().getEngineSettings()) {
        final int row = engineSettingsGrid.getRowCount();

        final Text engineSettingDescription = new Text(engineSetting.getText());
        engineSettingDescription.getStyleClass().add("captionTitle");

        final ObservableList<String> items = FXCollections.observableArrayList(engineSetting.getOptions());

        final ComboBox<String> engineSettingComboBox = new ComboBox<>(items);
        engineSettingComboBox.getStyleClass().add("engine-setting-combo-box");
        engineSettingComboBox.disableProperty().bind(getControl().lockEngineSettingsProperty());

        // if the container is not specified set no default values
        if (container != null) {
            try {
                engineSettingComboBox.setValue(engineSetting.getCurrentOption(container.getName()));
            } catch (Exception e) {
                engineSettingComboBox.getSelectionModel().select(0);
                LOGGER.warn(
                        String.format("Could not fetch current option for engine setting \"%s\", will use default.",
                                engineSetting.getText()));
                LOGGER.debug("Caused by: ", e);
            }
            engineSettingComboBox.valueProperty().addListener((Observable invalidation) -> Platform.runLater(() -> {
                getControl().setLockEngineSettings(true);

                engineSetting.setOption(container.getName(), items.indexOf(engineSettingComboBox.getValue()));

                getControl().setLockEngineSettings(false);
            }));
        }

        engineSettingsGrid.addRow(row, engineSettingDescription, engineSettingComboBox);
    }
}