下面列出了怎么用javafx.scene.control.ChoiceBox的API类实例代码及写法,或者点击链接到github查看源代码。
private void setFormConstraints(Node field) {
if (field instanceof ISetConstraints) {
((ISetConstraints) field).setFormConstraints(this);
} else if (field instanceof Button) {
_setFormConstraints((Button) field);
} else if (field instanceof TextField) {
_setFormConstraints((TextField) field);
} else if (field instanceof TextArea) {
_setFormConstraints((TextArea) field);
} else if (field instanceof ComboBox<?>) {
_setFormConstraints((ComboBox<?>) field);
} else if (field instanceof ChoiceBox<?>) {
_setFormConstraints((ChoiceBox<?>) field);
} else if (field instanceof CheckBox) {
_setFormConstraints((CheckBox) field);
} else if (field instanceof Spinner<?>) {
_setFormConstraints((Spinner<?>) field);
} else if (field instanceof VBox) {
_setFormConstraints((VBox) field);
} else if (field instanceof Label) {
_setFormConstraints((Label) field);
} else {
LOGGER.info("FormPane.setFormConstraints(): unknown field type: " + field.getClass().getName());
}
}
@Test
public void selectOptionWithQuotes() {
@SuppressWarnings("unchecked")
ChoiceBox<String> choiceBox = (ChoiceBox<String>) getPrimaryStage().getScene().getRoot().lookup(".choice-box");
LoggingRecorder lr = new LoggingRecorder();
Platform.runLater(() -> {
RFXChoiceBox rfxChoiceBox = new RFXChoiceBox(choiceBox, null, null, lr);
choiceBox.getItems().add(" \"Mouse \" ");
choiceBox.getSelectionModel().select(" \"Mouse \" ");
rfxChoiceBox.focusLost(null);
});
List<Recording> recordings = lr.waitAndGetRecordings(1);
Recording recording = recordings.get(0);
AssertJUnit.assertEquals("recordSelect", recording.getCall());
AssertJUnit.assertEquals(" \"Mouse \" ", recording.getParameters()[0]);
}
@Test
public void htmlOptionSelect() {
@SuppressWarnings("unchecked")
ChoiceBox<String> choiceBox = (ChoiceBox<String>) getPrimaryStage().getScene().getRoot().lookup(".choice-box");
LoggingRecorder lr = new LoggingRecorder();
String text = "This is a test text";
final String htmlText = "<html><font color=\"RED\"><h1><This is also content>" + text + "</h1></html>";
Platform.runLater(() -> {
RFXChoiceBox rfxChoiceBox = new RFXChoiceBox(choiceBox, null, null, lr);
choiceBox.getItems().add(htmlText);
choiceBox.getSelectionModel().select(htmlText);
rfxChoiceBox.focusLost(null);
});
List<Recording> recordings = lr.waitAndGetRecordings(1);
Recording recording = recordings.get(0);
AssertJUnit.assertEquals("recordSelect", recording.getCall());
AssertJUnit.assertEquals(text, recording.getParameters()[0]);
}
@Test
public void getText() {
ChoiceBox<?> choiceBox = (ChoiceBox<?>) getPrimaryStage().getScene().getRoot().lookup(".choice-box");
LoggingRecorder lr = new LoggingRecorder();
List<String> text = new ArrayList<>();
Platform.runLater(() -> {
RFXChoiceBox rfxChoiceBox = new RFXChoiceBox(choiceBox, null, null, lr);
choiceBox.getSelectionModel().select(1);
rfxChoiceBox.focusLost(null);
text.add(rfxChoiceBox._getText());
});
new Wait("Waiting for choice box text.") {
@Override
public boolean until() {
return text.size() > 0;
}
};
AssertJUnit.assertEquals("Cat", text.get(0));
}
@Ignore
public void testUpScalingQuality() throws Exception {
for (EScalingAlgorithm algo : EScalingAlgorithm.getAllEnabled()) {
if (algo.getSupportedForType().contains(EScalingAlgorithm.Type.UPSCALING)) {
ChoiceBox choiceBox = (ChoiceBox) scene.lookup("#choiceUpScale");
//choiceBox.getSelectionModel().
for (Object o : choiceBox.getItems()) {
if (o.toString().equals(algo.toString())) {
}
}
clickOn("#choiceUpScale").clickOn(algo.toString());
assertEquals("arguments should match", defaultBuilder.upScaleAlgorithm(algo).build(), controller.getFromUI(false));
}
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
markdownRendererLabel = new Label();
markdownRendererChoiceBox = new ChoiceBox<>();
markdownExtensionsLabel = new Label();
markdownExtensionsPane = new MarkdownExtensionsPane();
//======== this ========
setLayout("insets dialog");
setCols("[][grow,fill]");
setRows("[]para[][grow,fill]");
//---- markdownRendererLabel ----
markdownRendererLabel.setText(Messages.get("MarkdownOptionsPane.markdownRendererLabel.text"));
add(markdownRendererLabel, "cell 0 0");
add(markdownRendererChoiceBox, "cell 1 0,alignx left,growx 0");
//---- markdownExtensionsLabel ----
markdownExtensionsLabel.setText(Messages.get("MarkdownOptionsPane.markdownExtensionsLabel.text"));
add(markdownExtensionsLabel, "cell 0 1 2 1");
add(markdownExtensionsPane, "pad 0 indent 0 0,cell 0 2 2 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
UiFormantGrid( EWI4000sPatch editPatch, MidiHandler midiHandler ) {
setId( "editor-grid" );
Label mainLabel = new Label( "Formant Filter" );
mainLabel.setId( "editor-section-label" );
GridPane.setValignment( mainLabel, VPos.TOP );
add( mainLabel, 0, 0 );
formantChoice = new ChoiceBox<String>();
formantChoice.getItems().addAll( "Off", "Woodwind", "Strings" );
formantChoice.setOnAction( (event) -> {
midiHandler.sendLiveControl( 5, 81, formantChoice.getSelectionModel().getSelectedIndex() );
editPatch.formantFilter = formantChoice.getSelectionModel().getSelectedIndex();
});
add( formantChoice, 0, 1 );
}
UiKeyTriggerGrid( EWI4000sPatch editPatch, MidiHandler midiHandler ) {
setId( "editor-grid" );
Label mainLabel = new Label( "Key Trigger" );
mainLabel.setId( "editor-section-label" );
GridPane.setColumnSpan( mainLabel, 2 );
add( mainLabel, 0, 0 );
keyTriggerChoice = new ChoiceBox<String>();
keyTriggerChoice.getItems().addAll( "Single", "Multi" );
keyTriggerChoice.setOnAction( (event) -> {
midiHandler.sendLiveControl( 7, 81, keyTriggerChoice.getSelectionModel().getSelectedIndex() );
editPatch.formantFilter = keyTriggerChoice.getSelectionModel().getSelectedIndex();
});
add( keyTriggerChoice, 0, 1 );
}
public final void initializePreprocessedCorpusList(){
preprocessedCorpusList = new ChoiceBox();
UIUtils.setSize(preprocessedCorpusList, Main.columnWidthRIGHT, 24);
preprocessedCorpusList.setItems(AppParameters.dataset.preprocessedCorpusList);
preprocessedCorpusList.valueProperty().addListener(new ChangeListener<String>() {
@Override public void changed(ObservableValue ov, String t, String t1) {
clearFilterUI();
if(t1 != null){
LogUI.addLogEntry("Loading '"+AppParameters.dataset.id+"' ("+t1+")... ");
AppParameters.dataset.corpus.loadFrequencies(t1);
AppParameters.timeSliceA = 0;
AppParameters.timeSliceB = AppParameters.dataset.corpus.messageDistribution.length;
LogUI.addLogEntry("Done.");
resizeSlider.setMin(0);
resizeSlider.setLowValue(0);
resizeSlider.setMax(AppParameters.dataset.corpus.getLength());
resizeSlider.setHighValue(AppParameters.dataset.corpus.getLength());
}
}
});
}
protected void addListenersToNode(Node node) {
if (node instanceof TextField) {
TextField textField = (TextField) node;
textField.textProperty().addListener(((observable, oldValue, newValue) -> {
parametersChanged();
}));
}
if (node instanceof ComboBox) {
ComboBox<?> comboComp = (ComboBox<?>) node;
comboComp.valueProperty()
.addListener(((observable, oldValue, newValue) -> parametersChanged()));
}
if (node instanceof ChoiceBox) {
ChoiceBox<?> choiceBox = (ChoiceBox) node;
choiceBox.valueProperty()
.addListener(((observable, oldValue, newValue) -> parametersChanged()));
}
if (node instanceof CheckBox) {
CheckBox checkBox = (CheckBox) node;
checkBox.selectedProperty()
.addListener(((observable, oldValue, newValue) -> parametersChanged()));
}
if (node instanceof Region) {
Region panelComp = (Region)
node;
for (int i = 0; i < panelComp.getChildrenUnmodifiable().size(); i++) {
Node child =
panelComp.getChildrenUnmodifiable().get(i);
if (!(child instanceof Control)) {
continue;
}
addListenersToNode(child);
}
}
}
public void changed(ChoiceboxEntry o, ChoiceboxEntry n, ChoiceBox<ChoiceboxEntry> choiceBox) {
if (n != null)
n.invoke();
if (o != null && n != null && !n.isSelectable()) {
//select old value, if this one is unselectable
Platform.runLater(() -> choiceBox.setValue(o));
}
}
/***************************************************************************
* *
* ChoiceBox convenience *
* *
**************************************************************************/
static <T> void updateItem(final Cell<T> cell,
final StringConverter<T> converter,
final ChoiceBox<T> choiceBox) {
updateItem(cell, converter, null, null, choiceBox);
}
static <T> void updateItem(final Cell<T> cell,
final StringConverter<T> converter,
final HBox hbox,
final Node graphic,
final ChoiceBox<T> choiceBox) {
if (cell.isEmpty()) {
cell.setText(null);
cell.setGraphic(null);
} else {
if (cell.isEditing()) {
if (choiceBox != null) {
choiceBox.getSelectionModel().select(cell.getItem());
}
cell.setText(null);
if (graphic != null) {
hbox.getChildren().setAll(graphic, choiceBox);
cell.setGraphic(hbox);
} else {
cell.setGraphic(choiceBox);
}
} else {
cell.setText(getItemText(cell, converter));
cell.setGraphic(graphic);
}
}
}
static <T> ChoiceBox<T> createChoiceBox(
final Cell<T> cell,
final ObservableList<T> items,
final ObjectProperty<StringConverter<T>> converter) {
ChoiceBox<T> choiceBox = new ChoiceBox<T>(items);
choiceBox.setMaxWidth(Double.MAX_VALUE);
choiceBox.converterProperty().bind(converter);
choiceBox.showingProperty().addListener(o -> {
if (!choiceBox.isShowing()) {
cell.commitEdit(choiceBox.getSelectionModel().getSelectedItem());
}
});
return choiceBox;
}
public ChoiceBoxSample() {
super(150,100);
ChoiceBox cb = new ChoiceBox();
cb.getItems().addAll("Dog", "Cat", "Horse");
cb.getSelectionModel().selectFirst();
getChildren().add(cb);
}
public void addIELogLevel() {
ieLogLevel = new ChoiceBox<>();
ieLogLevel.getItems().add(null);
ieLogLevel.getItems().addAll(FXCollections.observableArrayList(InternetExplorerDriverLogLevel.values()));
String value = BrowserConfig.instance().getValue(getBrowserName(), "webdriver-ie-log-level");
if (value != null)
ieLogLevel.getSelectionModel().select(InternetExplorerDriverLogLevel.valueOf(value));
advancedPane.addFormField("Log Level:", ieLogLevel);
}
public void addUnexpectedAlertBehavior() {
unexpectedAlertBehaviour = new ChoiceBox<>();
unexpectedAlertBehaviour.getItems().add(null);
unexpectedAlertBehaviour.getItems().addAll(FXCollections.observableArrayList(UnexpectedAlertBehaviour.values()));
String value = BrowserConfig.instance().getValue(getBrowserName(), "browser-unexpected-alert-behaviour");
if (value != null)
unexpectedAlertBehaviour.getSelectionModel().select(UnexpectedAlertBehaviour.fromString(value));
advancedPane.addFormField("Unexpected alert behaviour:", unexpectedAlertBehaviour);
}
public void addPageLoadStrategy() {
pageLoadStrategy = new ChoiceBox<>();
pageLoadStrategy.getItems().add(null);
pageLoadStrategy.getItems().addAll(FXCollections.observableArrayList(PageLoadStrategy.values()));
String value = BrowserConfig.instance().getValue(getBrowserName(), "browser-page-load-strategy");
if (value != null)
pageLoadStrategy.getSelectionModel().select(PageLoadStrategy.fromString(value));
advancedPane.addFormField("Page load strategy:", pageLoadStrategy);
}
public String getChoiceBoxText(ChoiceBox<?> choiceBox, int index) {
if (index == -1) {
return null;
}
String original = getChoiceBoxItemText(choiceBox, index);
String itemText = original;
int suffixIndex = 0;
for (int i = 0; i < index; i++) {
String current = getChoiceBoxItemText(choiceBox, i);
if (current.equals(original)) {
itemText = String.format("%s(%d)", original, ++suffixIndex);
}
}
return itemText;
}
@SuppressWarnings("unchecked")
private String getChoiceBoxItemText(@SuppressWarnings("rawtypes")
ChoiceBox choiceBox, int index) {
@SuppressWarnings("rawtypes")
StringConverter converter = choiceBox.getConverter();
String text = null;
if (converter == null) {
text = choiceBox.getItems().get(index).toString();
} else {
text = converter.toString(choiceBox.getItems().get(index));
}
return stripHTMLTags(text);
}
public String[][] getContent(ChoiceBox<?> choiceBox) {
int nOptions = choiceBox.getItems().size();
String[][] content = new String[1][nOptions];
for (int i = 0; i < nOptions; i++) {
content[0][i] = getChoiceBoxText(choiceBox, i);
}
return content;
}
public int getChoiceBoxItemIndex(ChoiceBox<?> choiceBox, String value) {
ObservableList<?> items = choiceBox.getItems();
for (int i = 0; i < items.size(); i++) {
String text = getChoiceBoxText(choiceBox, i);
if (text.equals(value)) {
return i;
}
}
return -1;
}
public static void reset() {
add(Node.class, JavaFXElement.class);
add(TextInputControl.class, JavaFXTextInputControlElement.class);
add(HTMLEditor.class, JavaFXHTMLEditor.class);
add(CheckBox.class, JavaFXCheckBoxElement.class);
add(ToggleButton.class, JavaFXToggleButtonElement.class);
add(Slider.class, JavaFXSliderElement.class);
add(Spinner.class, JavaFXSpinnerElement.class);
add(SplitPane.class, JavaFXSplitPaneElement.class);
add(ProgressBar.class, JavaFXProgressBarElement.class);
add(ChoiceBox.class, JavaFXChoiceBoxElement.class);
add(ColorPicker.class, JavaFXColorPickerElement.class);
add(ComboBox.class, JavaFXComboBoxElement.class);
add(DatePicker.class, JavaFXDatePickerElement.class);
add(TabPane.class, JavaFXTabPaneElement.class);
add(ListView.class, JavaFXListViewElement.class);
add(TreeView.class, JavaFXTreeViewElement.class);
add(TableView.class, JavaFXTableViewElement.class);
add(TreeTableView.class, JavaFXTreeTableViewElement.class);
add(CheckBoxListCell.class, JavaFXCheckBoxListCellElement.class);
add(ChoiceBoxListCell.class, JavaFXChoiceBoxCellElement.class);
add(ComboBoxListCell.class, JavaFXComboBoxCellElement.class);
add(CheckBoxTreeCell.class, JavaFXCheckBoxTreeCellElement.class);
add(ChoiceBoxTreeCell.class, JavaFXChoiceBoxCellElement.class);
add(ComboBoxTreeCell.class, JavaFXComboBoxCellElement.class);
add(TableCell.class, JavaFXTableViewCellElement.class);
add(CheckBoxTableCell.class, JavaFXCheckBoxTableCellElement.class);
add(ChoiceBoxTableCell.class, JavaFXChoiceBoxCellElement.class);
add(ComboBoxTableCell.class, JavaFXComboBoxCellElement.class);
add(TreeTableCell.class, JavaFXTreeTableCellElement.class);
add(CheckBoxTreeTableCell.class, JavaFXCheckBoxTreeTableCell.class);
add(ChoiceBoxTreeTableCell.class, JavaFXChoiceBoxCellElement.class);
add(ComboBoxTreeTableCell.class, JavaFXComboBoxCellElement.class);
add(WebView.class, JavaFXWebViewElement.class);
add(GenericStyledArea.GENERIC_STYLED_AREA_CLASS, RichTextFXGenericStyledAreaElement.class);
}
@Override
public List<IJavaFXElement> getByPseudoElement(String selector, Object[] params) {
if (selector.equals("nth-option")) {
return Arrays.asList(new JavaFXChoiceBoxOptionElement(this, ((Integer) params[0]).intValue() - 1));
} else if (selector.equals("all-options") || selector.equals("all-cells")) {
ChoiceBox<?> listView = (ChoiceBox<?>) getComponent();
ArrayList<IJavaFXElement> r = new ArrayList<>();
int nItems = listView.getItems().size();
for (int i = 0; i < nItems; i++) {
r.add(new JavaFXChoiceBoxOptionElement(this, i));
}
return r;
}
return super.getByPseudoElement(selector, params);
}
@Override
public boolean marathon_select(String value) {
ChoiceBox<?> choiceBox = (ChoiceBox<?>) getComponent();
String text = stripHTMLTags(value);
int selectedItem = getChoiceBoxItemIndex(choiceBox, text);
if (selectedItem == -1) {
return false;
}
choiceBox.getSelectionModel().select(selectedItem);
return true;
}
@Test
public void select() {
ChoiceBox<?> choiceBoxNode = (ChoiceBox<?>) getPrimaryStage().getScene().getRoot().lookup(".choice-box");
Platform.runLater(() -> {
choiceBox.marathon_select("Cat");
});
new Wait("Waiting for choice box option to be set.") {
@Override
public boolean until() {
return choiceBoxNode.getSelectionModel().getSelectedIndex() == 1;
}
};
}
public ChoiceBoxSample() {
super(150,100);
ChoiceBox cb = new ChoiceBox();
cb.getItems().addAll("Dog", "Cat", "Horse");
cb.getSelectionModel().selectFirst();
getChildren().add(cb);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
scene.setFill(Color.ALICEBLUE);
stage.setScene(scene);
stage.show();
stage.setTitle("ChoiceBox Sample");
stage.setWidth(300);
stage.setHeight(200);
label.setFont(Font.font("Arial", 25));
label.setLayoutX(40);
final String[] greetings = new String[] { "Hello", "Hola", "Привет", "你好", "こんにちは" };
final ChoiceBox cb = new ChoiceBox(FXCollections.observableArrayList("English", "Español", "Русский", "简体中文", "日本語"));
cb.getSelectionModel().selectedIndexProperty()
.addListener((ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> {
label.setText(greetings[new_val.intValue()]);
});
cb.setTooltip(new Tooltip("Select the language"));
cb.setValue("English");
HBox hb = new HBox();
hb.getChildren().addAll(cb, label);
hb.setSpacing(30);
hb.setAlignment(Pos.CENTER);
hb.setPadding(new Insets(10, 0, 0, 10));
((Group) scene.getRoot()).getChildren().add(hb);
}
public void record(ActionEvent event) {
MenuItem source = (MenuItem) event.getSource();
String tagForMenu = getTagForMenu(source);
String menuPath = getSelectedMenuPath(source);
if (!(ownerNode instanceof ChoiceBox<?>) && ownerNode != null) {
recorder.recordSelectMenu(new RFXUnknownComponent(ownerNode, oMapConfig, null, recorder), tagForMenu, menuPath);
}
}
@Override
public void focusLost(RFXComponent next) {
ChoiceBox<?> choiceBox = (ChoiceBox<?>) node;
Object selectedItem = choiceBox.getSelectionModel().getSelectedItem();
if (selectedItem != null && selectedItem.equals(prevSelectedItem)) {
return;
}
String text = getChoiceBoxText(choiceBox, choiceBox.getSelectionModel().getSelectedIndex());
if (text != null) {
recorder.recordSelect(this, text);
}
}