下面列出了javafx.scene.control.ComboBox#setOnAction ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void addGroup(String title, ObservableList<Class<? extends Task>> list) {
final Tuple2<Label, ComboBox<Class<? extends Task>>> selectTaskToIntercept =
addTopLabelComboBox(root, ++rowIndex, title, "Select task to intercept", 15);
ComboBox<Class<? extends Task>> comboBox = selectTaskToIntercept.second;
comboBox.setVisibleRowCount(list.size());
comboBox.setItems(list);
comboBox.setConverter(new StringConverter<>() {
@Override
public String toString(Class<? extends Task> item) {
return item.getSimpleName();
}
@Override
public Class<? extends Task> fromString(String s) {
return null;
}
});
comboBox.setOnAction(event -> Task.taskToIntercept = comboBox.getSelectionModel().getSelectedItem());
}
public static Dialog showFontPicker(BindableValue<Font> fontProperty) {
ComboBox<String> fontNames = new ComboBox<>(
observableList(Font.getFontNames().stream().distinct().collect(toList())));
fontNames.getSelectionModel().select(fontProperty.getValue().getName());
ComboBox<Double> fontSizes = new ComboBox<>(
observableList(rangeClosed(6, 42)
.asDoubleStream().boxed()
.collect(toList()))
);
fontSizes.getSelectionModel().select(fontProperty.getValue().getSize());
EventHandler<ActionEvent> eventHandler = (event) ->
fontProperty.setValue(new Font(fontNames.getValue(), fontSizes.getValue()));
fontNames.setOnAction(eventHandler);
fontSizes.setOnAction(eventHandler);
GridPane grid = new GridPane();
grid.setVgap(5);
grid.add(new Label("Font name:"), 0, 0);
grid.add(fontNames, 1, 0);
grid.add(new Label("Font size:"), 0, 1);
grid.add(fontSizes, 1, 1);
Dialog dialog = new Dialog(grid);
dialog.setTitle("Select a font");
dialog.setAlwaysOnTop(true);
dialog.setResizable(false);
dialog.makeTransparentWhenLoseFocus();
dialog.show();
return dialog;
}
public static Tuple2<ComboBox<TradeCurrency>, Integer> addRegionCountryTradeCurrencyComboBoxes(GridPane gridPane,
int gridRow,
Consumer<Country> onCountrySelectedHandler,
Consumer<TradeCurrency> onTradeCurrencySelectedHandler) {
gridRow = addRegionCountry(gridPane, gridRow, onCountrySelectedHandler);
ComboBox<TradeCurrency> currencyComboBox = FormBuilder.addComboBox(gridPane, ++gridRow,
Res.get("shared.currency"));
currencyComboBox.setPromptText(Res.get("list.currency.select"));
currencyComboBox.setItems(FXCollections.observableArrayList(CurrencyUtil.getAllSortedFiatCurrencies()));
currencyComboBox.setConverter(new StringConverter<>() {
@Override
public String toString(TradeCurrency currency) {
return currency.getNameAndCode();
}
@Override
public TradeCurrency fromString(String string) {
return null;
}
});
currencyComboBox.setDisable(true);
currencyComboBox.setOnAction(e ->
onTradeCurrencySelectedHandler.accept(currencyComboBox.getSelectionModel().getSelectedItem()));
return new Tuple2<>(currencyComboBox, gridRow);
}
void setCountryComboBoxAction(ComboBox<Country> countryComboBox, CountryBasedPaymentAccount paymentAccount) {
countryComboBox.setOnAction(e -> {
Country selectedItem = countryComboBox.getSelectionModel().getSelectedItem();
paymentAccount.setCountry(selectedItem);
updateCountriesSelection(euroCountryCheckBoxes);
updateCountriesSelection(nonEuroCountryCheckBoxes);
updateFromInputs();
});
}
void setCountryComboBoxAction(ComboBox<Country> countryComboBox) {
countryComboBox.setOnAction(e -> {
selectedCountry = countryComboBox.getSelectionModel().getSelectedItem();
updateFromInputs();
accountIdInputTextField.resetValidation();
accountIdInputTextField.validate();
accountIdInputTextField.requestFocus();
countryComboBox.requestFocus();
});
}
@Override
public ComboBox<String> createJFXNode() throws Exception
{
final ComboBox<String> combo = new ComboBox<>();
if (! toolkit.isEditMode())
{
// 'editable' cannot be changed at runtime
combo.setEditable(model_widget.propEditable().getValue());
// Handle user's selection
combo.setOnAction((event)->
{ // We are updating the UI, ignore
if (active)
return;
final String value = combo.getValue();
if (value != null)
{
// Restore current value
contentChanged(null, null, null);
// ... which should soon be replaced by updated value, if accepted
Platform.runLater(() -> confirm(value));
}
});
// Also write to PV when user re-selects the current value
combo.setCellFactory(list ->
{
final ListCell<String> cell = new ListCell<>()
{
@Override
public void updateItem(final String item, final boolean empty)
{
super.updateItem(item, empty);
if ( !empty )
setText(item);
}
};
cell.addEventFilter(MouseEvent.MOUSE_PRESSED, e ->
{
// Is this a click on the 'current' value which would by default be ignored?
if (Objects.equals(combo.getValue(), cell.getItem()))
{
combo.fireEvent(new ActionEvent());
// Alternatively...
//combo.setValue(null);
//combo.getSelectionModel().select(cell.getItem());
e.consume();
}
});
return cell;
});
combo.setOnMouseClicked(event -> {
// Secondary mouse button should bring up context menu,
// but not show selections (i.e. not expand drop-down).
if(event.getButton().equals(MouseButton.SECONDARY)){
combo.hide();
}
});
}
contentChanged(null, null, null);
return combo;
}
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);
}
@Override
public void startEdit()
{
super.startEdit();
if (! isEditing())
return;
setText(null);
final TableItemProxy proxy = getTableView().getItems().get(getIndex());
final VType value = proxy.getItem().getValue();
if (value instanceof VEnum)
{
// Use combo for Enum-valued data
final VEnum enumerated = (VEnum) value;
final ComboBox<String> combo = new ComboBox<>();
combo.getItems().addAll(enumerated.getDisplay().getChoices());
combo.getSelectionModel().select(enumerated.getIndex());
combo.setOnAction(event ->
{
// Need to write String, using the enum index
commitEdit(Integer.toString(combo.getSelectionModel().getSelectedIndex()));
event.consume();
});
combo.setOnKeyReleased(event ->
{
if (event.getCode() == KeyCode.ESCAPE)
{
cancelEdit();
event.consume();
}
});
setGraphic(combo);
Platform.runLater(() -> combo.requestFocus());
Platform.runLater(() -> combo.show());
}
else
{
final TextField text_field = new TextField(getItem());
text_field.setOnAction(event ->
{
commitEdit(text_field.getText());
event.consume();
});
text_field.setOnKeyReleased(event ->
{
if (event.getCode() == KeyCode.ESCAPE)
{
cancelEdit();
event.consume();
}
});
setGraphic(text_field);
text_field.selectAll();
text_field.requestFocus();
}
}
private WikiPane setContent() {
addElement("choose-columns-intro", 40);
/* templates */
rightContainer.getChildren().add(new WikiLabel("choose-columns-template").setClass("bold"));
Settings.TEMPLATES.forEach((key, value) -> {
Hyperlink label = new Hyperlink(key);
label.setOnAction(event -> {
Template template = Settings.TEMPLATES.get(Session.TEMPLATE);
for (TemplateField tf : template.variables) {
Settings.setSetting("var-" + tf.name + "-value", tf.value);
Settings.setSetting("var-" + tf.name + "-selection", tf.selection);
}
Session.METHOD = "template";
Session.TEMPLATE = key;
Settings.setSetting("template", Session.TEMPLATE);
if (prevLabel != null) {
prevLabel.getStyleClass().remove("bold");
}
prevLabel = label;
prevLabel.getStyleClass().add("bold");
showTemplateFields(Session.TEMPLATE);
});
rightContainer.getChildren().add(label);
});
/* advanced */
wikicodeLink = new Hyperlink(Util.text("choose-columns-wikicode"));
rightContainer.getChildren().addAll(
new Region(),
new WikiLabel("choose-columns-advanced").setClass("bold"),
wikicodeLink
);
addElementRow(templatePane, 10,
new Node[]{
new WikiScrollPane(rightContainer).setWidth(150),
new WikiScrollPane(templateDescContainer)
},
new Priority[]{Priority.NEVER, Priority.ALWAYS}
);
addElement(templatePane);
/* wiki code pane */
ComboBox templateBox = new ComboBox();
templateBox.getItems().addAll(Settings.TEMPLATES.keySet().toArray());
templateBox.setOnAction((Event ev) -> {
String templateName = templateBox
.getSelectionModel()
.getSelectedItem()
.toString();
Template t = Settings.TEMPLATES.get(templateName);
wikicodeText.setText(t.wikicode);
});
wikicodeText.getStyleClass().add("mw-ui-input");
wikicodeText.setPrefHeight(this.stage.getHeight() - 350);
wikicodeText.setText(Session.WIKICODE);
wikicodePane.getChildren().addAll(templateBox, wikicodeText);
this.stage.heightProperty().addListener((obs, oldVal, newVal) -> {
wikicodeText.setPrefHeight(this.stage.getHeight() - 350);
});
return this;
}