下面列出了怎么用javafx.scene.control.CheckBox的API类实例代码及写法,或者点击链接到github查看源代码。
public DataSourceTitledPane(final DataAccessPlugin plugin, final ImageView dataSourceIcon, final PluginParametersPaneListener top, final Set<String> globalParamLabels) {
this.plugin = plugin;
this.top = top;
this.globalParamLabels = globalParamLabels;
isLoaded = false;
enabled = new CheckBox();
label = new Label(plugin.getName(), dataSourceIcon);
setGraphic(createTitleBar());
enabled.setDisable(true);
final boolean isExpanded = DataAccessPreferences.isExpanded(plugin.getName(), false);
createParameters(isExpanded, null);
setPadding(Insets.EMPTY);
setTooltip(new Tooltip(plugin.getDescription()));
}
/**
* 艦種フィルタのチェックボックス
* @return 艦種フィルタのチェックボックス
*/
private List<CheckBox> typeCheckBox() {
return Arrays.asList(
this.escort,
this.destroyer,
this.lightCruiser,
this.torpedoCruiser,
this.heavyCruiser,
this.flyingDeckCruiser,
this.seaplaneTender,
this.escortCarrier,
this.carrier,
this.armoredcarrier,
this.battleship,
this.flyingDeckBattleship,
this.submarine,
this.carrierSubmarine,
this.landingship,
this.repairship,
this.submarineTender,
this.trainingShip,
this.supply);
}
@Override
protected Node createEditorControls() {
final GridPane controls = new GridPane();
controls.setAlignment(Pos.CENTER);
controls.setVgap(CONTROLS_DEFAULT_VERTICAL_SPACING);
numberField = new TextField();
numberField.textProperty().addListener((o, n, v) -> {
update();
});
noValueCheckBox = new CheckBox(NO_VALUE_LABEL);
noValueCheckBox.setAlignment(Pos.CENTER);
noValueCheckBox.selectedProperty().addListener((v, o, n) -> {
numberField.setDisable(noValueCheckBox.isSelected());
update();
});
controls.addRow(0, numberField);
controls.addRow(1, noValueCheckBox);
return controls;
}
@Test
public void getText() throws Throwable {
CheckBox checkBox = findCheckbox("Simple checkbox");
LoggingRecorder lr = new LoggingRecorder();
List<String> text = new ArrayList<>();
Platform.runLater(new Runnable() {
@Override
public void run() {
checkBox.setSelected(true);
RFXCheckBox rfxCheckBox = new RFXCheckBox(checkBox, null, null, lr);
rfxCheckBox.mouseClicked(null);
text.add(rfxCheckBox._getText());
}
});
new Wait("Waiting for checkbox text") {
@Override
public boolean until() {
return text.size() > 0;
}
};
AssertJUnit.assertEquals("Simple checkbox", text.get(0));
}
@Override
protected Node createEditorControls() {
final GridPane controls = new GridPane();
controls.setAlignment(Pos.CENTER);
controls.setVgap(CONTROLS_DEFAULT_VERTICAL_SPACING);
final HBox timeSpinnerContainer = createTimeSpinners();
noValueCheckBox = new CheckBox(NO_VALUE_LABEL);
noValueCheckBox.setAlignment(Pos.CENTER);
noValueCheckBox.selectedProperty().addListener((v, o, n) -> {
hourSpinner.setDisable(noValueCheckBox.isSelected());
minSpinner.setDisable(noValueCheckBox.isSelected());
secSpinner.setDisable(noValueCheckBox.isSelected());
milliSpinner.setDisable(noValueCheckBox.isSelected());
update();
});
controls.addRow(0, timeSpinnerContainer);
controls.addRow(1, noValueCheckBox);
return controls;
}
private int addTraceContent(final GridPane layout, int row, final Trace<?> trace)
{
Label label = new Label(trace.getName());
layout.add(label, 5, row);
final ColorPicker color = createPicker(trace.getColor());
color.setOnAction(event ->
{
trace.setColor(color.getValue());
plot.requestUpdate();
});
layout.add(color, 6, row);
final CheckBox visible = new CheckBox(Messages.PlotConfigVisible);
visible.setSelected(trace.isVisible());
visible.setOnAction(event ->
{
trace.setVisible(visible.isSelected());
plot.requestUpdate();
});
layout.add(visible, 7, row++);
return row;
}
@Override
public DataElt getDataElt(DataEltType type, String eltname, String objectfieldname) {
if (!(type instanceof MultipleChoiceDataEltType))
throw new RuntimeException(String.format(
"Only MultipleChoiceDataEltType can be extracted from CMultiChoiceField, but request was %s",
type));
MultipleChoiceDataElt<
CChoiceFieldValue> multipleelement = new MultipleChoiceDataElt<CChoiceFieldValue>(eltname);
for (int i = 0; i < checkboxpanel.size(); i++) {
CheckBox thischeckbox = checkboxpanel.get(i);
if (thischeckbox.isSelected()) {
multipleelement.addChoice(values.get(i));
}
}
return multipleelement;
}
public CheckBoxes() {
VBox vbox = new VBox();
vbox.setSpacing(10);
CheckBox cb1 = new CheckBox("Simple checkbox");
CheckBox cb2 = new CheckBox("Three state checkbox");
cb2.setAllowIndeterminate(true);
cb2.setIndeterminate(false);
CheckBox cb3 = new CheckBox("Disabled");
cb3.setSelected(true);
cb3.setDisable(true);
vbox.getChildren().add(cb1);
vbox.getChildren().add(cb2);
vbox.getChildren().add(cb3);
getChildren().add(vbox);
}
private Pane getAutoRangeCheckBoxes(final Axis axis) {
final Pane boxMax = new VBox();
VBox.setVgrow(boxMax, Priority.ALWAYS);
final CheckBox autoRanging = new CheckBox("auto ranging");
HBox.setHgrow(autoRanging, Priority.ALWAYS);
VBox.setVgrow(autoRanging, Priority.ALWAYS);
autoRanging.setMaxWidth(Double.MAX_VALUE);
autoRanging.setSelected(axis.isAutoRanging());
autoRanging.selectedProperty().bindBidirectional(axis.autoRangingProperty());
boxMax.getChildren().add(autoRanging);
final CheckBox autoGrow = new CheckBox("auto grow");
HBox.setHgrow(autoGrow, Priority.ALWAYS);
VBox.setVgrow(autoGrow, Priority.ALWAYS);
autoGrow.setMaxWidth(Double.MAX_VALUE);
autoGrow.setSelected(axis.isAutoGrowRanging());
autoGrow.selectedProperty().bindBidirectional(axis.autoGrowRangingProperty());
boxMax.getChildren().add(autoGrow);
return boxMax;
}
private Tab getChartTab(XYChart chart) {
final ParameterTab pane = new ParameterTab("Chart");
final CheckBox gridVisibleX = new CheckBox("");
gridVisibleX.setSelected(true);
chart.horizontalGridLinesVisibleProperty().bindBidirectional(gridVisibleX.selectedProperty());
pane.addToParameterPane("Show X-Grid: ", gridVisibleX);
final CheckBox gridVisibleY = new CheckBox("");
gridVisibleY.setSelected(true);
chart.verticalGridLinesVisibleProperty().bindBidirectional(gridVisibleY.selectedProperty());
pane.addToParameterPane("Show Y-Grid: ", gridVisibleY);
final CheckBox gridOnTop = new CheckBox("");
gridOnTop.setSelected(true);
chart.getGridRenderer().drawOnTopProperty().bindBidirectional(gridOnTop.selectedProperty());
pane.addToParameterPane("Grid on top: ", gridOnTop);
return pane;
}
void fillUpFlowPaneWithCountries(List<CheckBox> checkBoxList, FlowPane flowPane, Country country) {
final String countryCode = country.code;
CheckBox checkBox = new AutoTooltipCheckBox(countryCode);
checkBox.setUserData(countryCode);
checkBoxList.add(checkBox);
checkBox.setMouseTransparent(false);
checkBox.setMinWidth(45);
checkBox.setMaxWidth(45);
checkBox.setTooltip(new Tooltip(country.name));
checkBox.setOnAction(event -> {
if (checkBox.isSelected()) {
addAcceptedCountry(countryCode);
} else {
removeAcceptedCountry(countryCode);
}
updateAllInputsValid();
});
flowPane.getChildren().add(checkBox);
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void canToggleResourceModifiers(final boolean checked) {
final CheckBox checkBox = mock(CheckBox.class);
when(checkBox.isSelected()).thenReturn(checked);
final Node node1 = mock(Node.class);
final Node node2 = mockNodeInGridPane(0, 2);
final Node node3 = mockNodeInGridPane(0, 3);
final Node node4 = mockNodeInGridPane(1, 2);
final Node node5 = mockNodeInGridPane(1, 3);
final GridPane factionGrid = mock(GridPane.class);
when(factionGrid.getChildren())
.thenReturn(FXCollections.observableArrayList(node1, node2, node3, node4, node5));
roleSelection.setFactionGrid(factionGrid);
roleSelection.setResourceModifierCheckbox(checkBox);
roleSelection.toggleResourceModifiers();
verify(node1, never()).setDisable(anyBoolean());
verify(node2, never()).setDisable(anyBoolean());
verify(node3, never()).setDisable(anyBoolean());
verify(node4, never()).setDisable(anyBoolean());
verify(node5).setDisable(!checked);
}
@Override
public void start(Stage primaryStage) throws Exception {
// controls
WebView webView = createWebView();
Label urlLabel = createUrlLabel();
CheckBox listenerAttachedBox = createListenerAttachedBox();
CheckBox cancelEventBox = createCancelEventBox();
// listener
WebViewHyperlinkListener listener = event -> {
showEventOnLabel(event, urlLabel);
return cancelEventBox.isSelected();
};
manageListener(webView, listener, listenerAttachedBox.selectedProperty());
// put together
VBox box = new VBox(webView, listenerAttachedBox, cancelEventBox, urlLabel);
java.net.CookieHandler.setDefault(null);
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.show();
}
public BooleanCell() {
checkBox = new CheckBox();
checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(isEditing()) {
commitEdit(newValue == null ? false : newValue);
}
}
});
this.setGraphic(checkBox);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.setEditable(true);
}
@Override
public void updateItem(Layer layer, boolean empty) {
super.updateItem(layer, empty);
if (!empty) {
// use the layer's name for the text
setText(layer.getName());
// add a check box to allow the user to change the visibility of the layer
CheckBox checkBox = new CheckBox();
setGraphic(checkBox);
// toggle the layer's visibility when the check box is toggled
checkBox.setSelected(layer.isVisible());
checkBox.selectedProperty().addListener(e -> layer.setVisible(checkBox.isSelected()));
} else {
setText(null);
setGraphic(null);
}
}
public SelectableTitledPane(String title, Node content) {
super(title, content);
checkBox = new CheckBox(title);
checkBox.selectedProperty().
bindBidirectional(this.expandedProperty());
setExpanded(false);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(checkBox);
//setSkin(new TitledPaneSkin(this));
lookup(".arrow").
setVisible(false);
lookup(".title").
setStyle("-fx-padding: 0 0 4 -10;"
+ "-fx-font-color: white;"
+ "-fx-background-color: black;");
lookup(".content").
setStyle("-fx-background-color: black;"
+ "-fx-font-color: white;"
+ "-fx-font-smoothing-type: lcd;"
+ "-fx-padding: 0.2em 0.2em 0.2em 1.316667em;");
}
public CustomDemoNode(Chart3D chart) {
this.chartViewer = new Chart3DViewer(chart);
this.chartViewer.addEventHandler(FXChart3DMouseEvent.MOUSE_CLICKED,
(FXChart3DMouseEvent event) -> {
chartMouseClicked(event);
});
this.selectedRowKey = "Apple";
this.selectedColumnKey = "Q4/12";
this.itemLabelCheckBox = new CheckBox("Show item labels?");
this.itemLabelCheckBox.setOnAction(e -> {
updateItemSelection(selectedRowKey, selectedColumnKey);
chart.setNotify(true);
});
setCenter(this.chartViewer);
HBox container = new HBox();
container.setAlignment(Pos.CENTER);
container.setPadding(new Insets(4.0, 4.0, 4.0, 4.0));
container.getChildren().add(this.itemLabelCheckBox);
setBottom(container);
}
@Override
public void updateItem(ImageItem item, boolean empty) {
super.updateItem(item, empty);
setText(null);
if (empty || item == null) {
setGraphic(null);
return;
}
try {
CheckBox cb = (CheckBox) getGraphic();
cb.setText(null);
cb.setGraphic(item.makeNode(imageSize));
} catch (Exception e) {
setGraphic(null);
}
}
public static Alert create(final String title,
final String header,
final String htmlContent,
final String checkboxContentKey,
final PropertyContext context,
final String option
)
{
GuiAssertions.assertIsNotJavaFXThread();
Objects.requireNonNull(title);
Objects.requireNonNull(header);
Objects.requireNonNull(htmlContent);
Objects.requireNonNull(context);
Objects.requireNonNull(option);
Alert alert = GuiUtility.runOnJavaFXThreadNow(() -> new Alert(Alert.AlertType.INFORMATION));
alert.setTitle(title);
alert.setContentText(null);
alert.setHeaderText(header);
CheckBox showLicense = new CheckBox(LanguageBundle.getString(checkboxContentKey));
showLicense.selectedProperty().addListener((observableValue, oldValue, newValue) ->
context.setBoolean(option, newValue));
showLicense.setSelected(context.getBoolean(option));
Platform.runLater(() -> {
WebView webView = new WebView();
webView.getEngine().loadContent(htmlContent);
alert.getDialogPane().setContent(webView);
});
alert.getDialogPane().setExpandableContent(showLicense);
return alert;
}
/** Sets the field's text based on the selected items list. */
private void setFieldText(ContextMenu dropDown, List<String> selectedItems, TextField field)
{
// Handle drop down menu item checking.
for (MenuItem menuItem : dropDown.getItems())
{
CustomMenuItem custom = (CustomMenuItem) menuItem;
CheckBox check = (CheckBox) custom.getContent();
// If the item is selected make sure it is checked.
if (selectedItems.contains(check.getText()))
{
if (! check.isSelected())
check.setSelected(true);
}
// If the item is not selected, make sure it is not checked.
else
{
if (check.isSelected())
check.setSelected(false);
}
}
// Build the field text string.
String fieldText = "";
for (String item : selectedItems)
{
fieldText += (fieldText.isEmpty() ? "" : ", ") + item;
}
field.setText(fieldText);
}
/**
* Creates a new {@link CheckBox} with containing the given {@link String text}
*
* @param text The text
* @return The new {@link CheckBox}
*/
protected static CheckBox createCheckBox(String text) {
final CheckBox checkBox = new CheckBox(text);
checkBox.getStyleClass().add("sidebarCheckBox");
return checkBox;
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Tooltip Sample");
stage.setWidth(330);
stage.setHeight(150);
total.setFont(new Font("Arial", 20));
for (int i = 0; i < rooms.length; i++) {
final CheckBox cb = cbs[i] = new CheckBox(rooms[i]);
final Integer rate = rates[i];
final Tooltip tooltip = new Tooltip("$" + rates[i].toString());
tooltip.setFont(new Font("Arial", 16));
cb.setTooltip(tooltip);
cb.selectedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) -> {
if (cb.isSelected()) {
sum = sum + rate;
} else {
sum = sum - rate;
}
total.setText("Total: $" + sum.toString());
});
}
VBox vbox = new VBox();
vbox.getChildren().addAll(cbs);
vbox.setSpacing(5);
HBox root = new HBox();
root.getChildren().add(vbox);
root.getChildren().add(total);
root.setSpacing(40);
root.setPadding(new Insets(20, 10, 10, 20));
((Group) scene.getRoot()).getChildren().add(root);
stage.setScene(scene);
stage.show();
}
public void setGraphicIsEnabled(boolean enabled) {
this.getChildren().remove(getChildren().size() - 1);
String lessVisibleText = "less-visible-tree-cell";
if (enabled) {
getChildren().add(rbSelected);
entry.getStyleClass().remove(lessVisibleText);
} else {
CheckBox checkBox = new CheckBox();
checkBox.setIndeterminate(true);
checkBox.setDisable(true);
getChildren().add(checkBox);
entry.getStyleClass().add(lessVisibleText);
}
}
@Test
public void reset() {
clickOn(".validable-container-field").write("Chuck");
Set<Node> nodes = lookup(n -> n instanceof CheckBox).queryAll();
nodes.forEach(n -> clickOn(n));
WaitForAsyncUtils.waitForAsyncFx(2000, () -> victim.resetView());
assertEquals("", lookup(".validable-container-field").queryAs(ValidableTextField.class).getText());
assertFalse(victim.overwrite().isSelected());
assertFalse(lookup("#discardBookmarksField").queryAs(CheckBox.class).isSelected());
assertTrue(lookup("#compressField").queryAs(CheckBox.class).isSelected());
}
private HBox clientBuilderSettingsLeft() {
HBox hBox = Styler.hContainer(20);
hBox.getStylesheets().add(getClass().getResource("/css/global.css").toExternalForm());
hBox.setId("clientBuilder");
hBox.setPadding(new Insets(20, 20, 20, 20));
Label title = (Label) Styler.styleAdd(new Label("Client Builder"), "title");
persistent = new CheckBox("Persistent");
autoSpread = new CheckBox("Auto-Spread");
hBox.getChildren().add(Styler.vContainer(20, title, persistent, autoSpread));
return hBox;
}
@Override
protected final CheckBox createJFXNode() throws Exception
{
final CheckBox checkbox = new CheckBox(label);
checkbox.setMinSize(ButtonBase.USE_PREF_SIZE, ButtonBase.USE_PREF_SIZE);
checkbox.setMnemonicParsing(false);
if (! toolkit.isEditMode())
checkbox.setOnAction(event -> handlePress());
return checkbox;
}
public OptionalParameterComponent(UserParameter<?, EmbeddedComponent> embeddedParameter) {
checkBox = new CheckBox();
checkBox.setOnAction(e -> {
boolean checkBoxSelected = checkBox.isSelected();
embeddedComponent.setDisable(!checkBoxSelected);
});
embeddedComponent = embeddedParameter.createEditingComponent();
embeddedComponent.setDisable(true);
getChildren().addAll(checkBox, embeddedComponent);
}
@Override
public void setOptionsBasedOnControls()
{
final GameMode gameMode = SettingsHandler.getGameAsProperty().get();
for (Map.Entry<RuleCheck, ButtonBase> settingsEntry : settings.entrySet())
{
// Save settings
if (gameMode.getModeContext().getReferenceContext().containsConstructedCDOMObject(
RuleCheck.class,
settingsEntry.getKey().getKeyName()
))
{
ButtonBase buttonBase = settingsEntry.getValue();
final boolean isSelected;
// see https://github.com/javafxports/openjdk-jfx/issues/494
if (buttonBase instanceof RadioButton)
{
isSelected = ((Toggle) buttonBase).selectedProperty().get();
}
else if (buttonBase instanceof CheckBox)
{
isSelected = ((CheckBox) buttonBase).selectedProperty().get();
}
else
{
throw new IllegalStateException("button base that isn't of the right type " + buttonBase);
}
SettingsHandler.setRuleCheck(settingsEntry.getKey().getKeyName(), isSelected);
}
}
}
@Test
public void selectCheckboxNotSelectedNotSelected() throws Throwable {
CheckBox checkBoxNode = (CheckBox) getPrimaryStage().getScene().getRoot().lookup(".check-box");
AssertJUnit.assertEquals(false, checkBoxNode.isSelected());
checkBox.marathon_select("unchecked");
new Wait("Waiting for the check box deselect.") {
@Override
public boolean until() {
return !checkBoxNode.isSelected();
}
};
}
public static ControlStyle getControlStyle(Node node) {
if (node == null || node.getId() == null) {
return null;
}
String id = node.getId();
ControlStyle style = null;
if (id.startsWith("his")) {
style = getHisControlStyle(id);
} else if (id.startsWith("settings")) {
style = getSettingsControlStyle(id);
} else if (id.startsWith("scope")) {
style = getScopeControlStyle(id);
} else if (id.startsWith("color")) {
style = getColorControlStyle(id);
} else if (id.startsWith("imageManu")) {
style = getImageManuControlStyle(id);
} else if (node instanceof ImageView) {
style = getImageViewStyle(id);
} else if (node instanceof RadioButton) {
style = getRadioButtonStyle(id);
} else if (node instanceof CheckBox) {
style = getCheckBoxStyle(id);
} else if (node instanceof ToggleButton) {
style = getToggleButtonStyle(id);
} else if (node instanceof Button) {
style = getButtonControlStyle(id);
}
return style;
}