下面列出了怎么用javafx.scene.control.ColorPicker的API类实例代码及写法,或者点击链接到github查看源代码。
public FontSpecsComponent() {
fontLabel = new Label();
fontSelectButton = new Button("Select font");
fontSelectButton.setOnAction(e -> {
var dialog = new FontSelectorDialog(currentFont);
var result = dialog.showAndWait();
if (result.isPresent())
setFont(result.get());
});
colorPicker = new ColorPicker(Color.BLACK);
getChildren().addAll(fontLabel, fontSelectButton, colorPicker);
}
public Parent createContent() {
final ColorPicker colorPicker = new ColorPicker(Color.GREEN);
final Label coloredText = new Label("Colors");
Font font = new Font(53);
coloredText.setFont(font);
final Button coloredButton = new Button("Colored Control");
Color c = colorPicker.getValue();
coloredText.setTextFill(c);
coloredButton.setStyle(createRGBString(c));
colorPicker.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Color newColor = colorPicker.getValue();
coloredText.setTextFill(newColor);
coloredButton.setStyle(createRGBString(newColor));
}
});
VBox outerVBox = new VBox(coloredText, coloredButton, colorPicker);
outerVBox.setAlignment(Pos.CENTER);
outerVBox.setSpacing(20);
outerVBox.setMaxSize(VBox.USE_PREF_SIZE, VBox.USE_PREF_SIZE);
return outerVBox;
}
/**
* {@inheritDoc}
*/
@Override
public void initializeParts() {
super.initializeParts();
node = new StackPane();
node.getStyleClass().add("simple-text-control");
colorPicker = new ColorPicker(initialValue);
colorPicker.setMaxWidth(Double.MAX_VALUE);
colorPicker.setOnAction(event -> {
if (!field.valueProperty().getValue().equals(colorPicker.getValue().toString())) {
field.valueProperty().setValue(colorPicker.getValue().toString());
}
});
field.valueProperty().setValue(colorPicker.getValue().toString());
fieldLabel = new Label(field.labelProperty().getValue());
}
/**
* {@inheritDoc}
*/
@Override
public void initializeParts() {
super.initializeParts();
node = new StackPane();
node.getStyleClass().add("simple-text-control");
colorPicker = new ColorPicker(initialValue);
colorPicker.setMaxWidth(Double.MAX_VALUE);
colorPicker.setOnAction(event -> {
if (!field.valueProperty().getValue().equals(getColorString(colorPicker.getValue())))
field.valueProperty().setValue(getColorString(colorPicker.getValue()));
});
field.valueProperty().setValue(getColorString(colorPicker.getValue()));
}
@Test
public void getText() {
ColorPicker colorPicker = (ColorPicker) getPrimaryStage().getScene().getRoot().lookup(".color-picker");
LoggingRecorder lr = new LoggingRecorder();
List<String> text = new ArrayList<>();
Platform.runLater(() -> {
RFXColorPicker rfxColorPicker = new RFXColorPicker(colorPicker, null, null, lr);
colorPicker.setValue(Color.rgb(234, 156, 44));
rfxColorPicker.focusLost(null);
text.add(rfxColorPicker._getText());
});
new Wait("Waiting for color picker text.") {
@Override
public boolean until() {
return text.size() > 0;
}
};
AssertJUnit.assertEquals("#ea9c2c", text.get(0));
}
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
@FxThread
protected void createComponents(@NotNull HBox container) {
super.createComponents(container);
colorPicker = new ColorPicker();
colorPicker.prefWidthProperty()
.bind(widthProperty().multiply(CONTROL_WIDTH_PERCENT));
FxControlUtils.onColorChange(colorPicker, this::updateValue);
FxUtils.addClass(colorPicker,
CssClasses.PROPERTY_CONTROL_COLOR_PICKER);
FxUtils.addChild(container, colorPicker);
}
public ColorTableCell(TableColumn<T, Color> column) {
colorPicker = new ColorPicker();
colorPicker.editableProperty().bind(column.editableProperty());
colorPicker.disableProperty().bind(column.editableProperty().not());
colorPicker.setOnShowing(event -> {
final TableView<T> tableView = getTableView();
tableView.getSelectionModel().select(getTableRow().getIndex());
tableView.edit(tableView.getSelectionModel().getSelectedIndex(), column);
});
colorPicker.valueProperty().addListener((observable, oldValue, newValue) -> {
if (isEditing()) {
commitEdit(newValue);
}
});
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
public ColorTableCell(TableColumn<T, Color> column) {
colorPicker = new ColorPicker();
colorPicker.editableProperty().bind(column.editableProperty());
colorPicker.disableProperty().bind(column.editableProperty().not());
colorPicker.setOnShowing(event -> {
final TableView<T> tableView = getTableView();
tableView.getSelectionModel().select(getTableRow().getIndex());
tableView.edit(tableView.getSelectionModel().getSelectedIndex(),
column);
});
colorPicker.valueProperty()
.addListener((observable, oldValue, newValue) -> {
commitEdit(newValue);
});
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
public void addColorControl(final String title,
final ObjectProperty<Paint> paintProperty) {
final ColorPicker colorPicker = ColorPickerBuilder.create()
.value((Color) paintProperty.get()).build();
paintProperty.bind(colorPicker.valueProperty());
final VBox box = new VBox();
final Text titleText = new Text(title);
titleText.textProperty().bind(new StringBinding() {
{
super.bind(colorPicker.valueProperty());
}
@Override
protected String computeValue() {
return title + " : " + colorPicker.getValue().toString();
}
});
box.getChildren().addAll(titleText, colorPicker);
getChildren().add(box);
}
@Override
public ColorPicker createEditingComponent() {
ColorPicker colorComponent = new ColorPicker(value);
// colorComponent.setBorder(BorderFactory.createCompoundBorder(colorComponent.getBorder(),
// BorderFactory.createEmptyBorder(0, 4, 0, 0)));
return colorComponent;
}
public ColorTableCell(TableColumn<T, Color> column) {
colorPicker = new ColorPicker();
colorPicker.editableProperty().bind(column.editableProperty());
colorPicker.disableProperty().bind(column.editableProperty().not());
colorPicker.setOnShowing(event -> {
final TableView<T> tableView = getTableView();
tableView.getSelectionModel().select(getTableRow().getIndex());
tableView.edit(tableView.getSelectionModel().getSelectedIndex(), column);
});
colorPicker.valueProperty().addListener((observable, oldValue, newValue) -> {
commitEdit(newValue);
});
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
@Override
public void setThemeForm(ColorPicker backgroundColorPicker, ComboBox<String> backgroundTypeSelect, TextField backgroundLocation, TextField backgroundVidLocation, Slider vidHueSlider, CheckBox vidStretchCheckbox) {
backgroundTypeSelect.getSelectionModel().select(LabelGrabber.INSTANCE.getLabel("color.theme.label"));
backgroundColorPicker.setValue(getColour());
backgroundLocation.clear();
backgroundVidLocation.clear();
vidHueSlider.setValue(0);
vidStretchCheckbox.setSelected(false);
}
@Override
public void setThemeForm(ColorPicker backgroundColorPicker, ComboBox<String> backgroundTypeSelect, TextField backgroundImgLocation, TextField backgroundVidLocation, Slider vidHueSlider, CheckBox vidStretchCheckbox) {
backgroundTypeSelect.getSelectionModel().select(LabelGrabber.INSTANCE.getLabel("image.theme.label"));
backgroundImgLocation.setText(imageName);
backgroundColorPicker.setValue(Color.BLACK);
backgroundColorPicker.fireEvent(new ActionEvent());
backgroundVidLocation.clear();
vidHueSlider.setValue(0);
vidStretchCheckbox.setSelected(false);
}
@Override
public void setThemeForm(ColorPicker backgroundColorPicker, ComboBox<String> backgroundTypeSelect, TextField backgroundImgLocation, TextField backgroundVidLocation, Slider vidHueSlider, CheckBox vidStretchCheckbox) {
backgroundTypeSelect.getSelectionModel().select(LabelGrabber.INSTANCE.getLabel("video.theme.label"));
backgroundVidLocation.setText(getVideoFile().getName());
vidHueSlider.setValue(hue);
vidStretchCheckbox.setSelected(stretch);
backgroundColorPicker.setValue(Color.BLACK);
backgroundColorPicker.fireEvent(new ActionEvent());
backgroundImgLocation.clear();
}
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 boolean marathon_select(String value) {
ColorPicker colorPicker = (ColorPicker) getComponent();
if (!value.equals("")) {
try {
colorPicker.setValue(Color.valueOf(value));
Event.fireEvent(colorPicker, new ActionEvent());
return true;
} catch (Throwable t) {
throw new IllegalArgumentException("Invalid value for '" + value + "' for color-picker '");
}
}
return false;
}
@Test
public void selectColor() {
ColorPicker colorPickerNode = (ColorPicker) getPrimaryStage().getScene().getRoot().lookup(".color-picker");
Platform.runLater(() -> colorpicker.marathon_select("#ff0000"));
new Wait("Waiting for color to be set.") {
@Override
public boolean until() {
return colorPickerNode.getValue().toString().equals("0xff0000ff");
}
};
}
@Override
public void focusLost(RFXComponent next) {
String currentColor = getColorCode(((ColorPicker) node).getValue());
if (!currentColor.equals(prevColor)) {
recorder.recordSelect(this, currentColor);
}
}
@Test
public void selectColor() {
ColorPicker colorPicker = (ColorPicker) getPrimaryStage().getScene().getRoot().lookup(".color-picker");
LoggingRecorder lr = new LoggingRecorder();
Platform.runLater(() -> {
RFXColorPicker rfxColorPicker = new RFXColorPicker(colorPicker, null, null, lr);
colorPicker.setValue(Color.rgb(234, 156, 44));
rfxColorPicker.focusLost(null);
});
List<Recording> recordings = lr.waitAndGetRecordings(1);
Recording recording = recordings.get(0);
AssertJUnit.assertEquals("recordSelect", recording.getCall());
AssertJUnit.assertEquals("#ea9c2c", recording.getParameters()[0]);
}
@Test
public void colorChooserWithColorName() {
ColorPicker colorPicker = (ColorPicker) getPrimaryStage().getScene().getRoot().lookup(".color-picker");
LoggingRecorder lr = new LoggingRecorder();
Platform.runLater(() -> {
RFXColorPicker rfxColorPicker = new RFXColorPicker(colorPicker, null, null, lr);
colorPicker.setValue(Color.RED);
rfxColorPicker.focusLost(null);
});
List<Recording> recordings = lr.waitAndGetRecordings(1);
Recording recording = recordings.get(0);
AssertJUnit.assertEquals("recordSelect", recording.getCall());
AssertJUnit.assertEquals("#ff0000", recording.getParameters()[0]);
}
/** @param section Segment of the color map
* @return ColorPicker that updates this segment in #color_sections
*/
private ColorPicker createColorPicker(final ColorSection section)
{
final Color color = section.color;
final int index = color_sections.indexOf(section);
if (index < 0)
throw new IllegalArgumentException("Cannot locate color section " + section);
final ColorPicker picker = new ColorPicker(color);
picker.setOnAction(event ->
{
color_sections.set(index, new ColorSection(section.value, picker.getValue()));
updateMapFromSections();
});
return picker;
}
static ColorPicker createPicker(final Color color)
{
final ColorPicker picker = new ColorPicker(color);
picker.getCustomColors().setAll(RGBFactory.PALETTE);
picker.setStyle("-fx-color-label-visible: false ;");
return picker;
}
/** Helper for CellValueFactory to create the picker for a color
* Cell value factory should then use picker.setOnAction() to
* handle edits.
* @param color
* @return
*/
static ColorPicker createPicker(final Color color)
{
final ColorPicker picker = new ColorPicker(color);
picker.getCustomColors().setAll(RGBFactory.PALETTE);
picker.setStyle("-fx-color-label-visible: false ;");
return picker;
}
private void initParts(){
paretoPanel = new ParetoPanel(createTestData1());
circleColor = new ColorPicker();
circleColor.setValue(Color.BLUE);
graphColor = new ColorPicker();
graphColor.setValue(Color.BLACK);
fontColor = new ColorPicker();
fontColor.setValue(Color.BLACK);
smoothing = new CheckBox("Smoothing");
realColor = new CheckBox("AutoSubColor");
showSubBars = new CheckBox("ShowSubBars");
singeSubBarCentered = new CheckBox("SingleSubBarCenterd");
circleSize = new Slider(1,60,20);
valueHeight = new Slider(0,80,20);
textHeight = new Slider(0,80,40);
barSpacing = new Slider(1,50,5);
pathHeight = new Slider(0,80,65);
barColors = new ArrayList<>();
backButton = new Button("Back to last layer");
exampeColorTheme = createRandomColorTheme(20);
paretoPanel.addColorTheme("example",exampeColorTheme);
colorTheme = new ComboBox<>();
colorTheme.getItems().addAll(paretoPanel.getColorThemeKeys());
mainBox = new HBox();
menu = new VBox();
pane = new Pane();
}
public void updateBarColorPicker(){
menu.getChildren().removeAll(barColors);
barColors.clear();
for(ParetoBar bar: paretoPanel.getParetoModel().getData()){
ColorPicker temp = new ColorPicker(bar.getFillColor());
barColors.add(temp);
bar.fillColorProperty().bindBidirectional(temp.valueProperty());
}
menu.getChildren().addAll(barColors);
}
@Override
@FxThread
protected void createComponents() {
super.createComponents();
colorPicker = new ColorPicker();
colorPicker.prefWidthProperty()
.bind(widthProperty().multiply(DEFAULT_FIELD_W_PERCENT));
FxControlUtils.onColorChange(colorPicker, this::change);
FxUtils.addClass(colorPicker, CssClasses.PROPERTY_CONTROL_COLOR_PICKER);
FxUtils.addChild(this, colorPicker);
}
public HBox ToggleButtonColor(String text,
ObjectProperty<Color> colorProperty,
BooleanProperty toggledProperty, boolean isToggled) {
HBox hbox = new HBox();
ToggleButton toggleButton = new ToggleButton(text);
toggleButton.setOnAction((ae) -> {
refreshAll();
});
ColorPicker colorPicker = new ColorPicker(colorProperty.get());
colorProperty.bind(colorPicker.valueProperty());
hbox.getChildren().addAll(toggleButton, colorPicker);
toggledProperty.bind(toggleButton.selectedProperty());
toggleButton.setSelected(isToggled);
return hbox;
}
private void updateColor() {
final ColorPicker colorPicker = (ColorPicker) getSkinnable();
Color color = colorPicker.getValue();
Color circleColor = color == null ? Color.WHITE : color;
// update picker box color
if (((JFXColorPicker) getSkinnable()).isDisableAnimation()) {
JFXNodeUtils.updateBackground(colorBox.getBackground(), colorBox, circleColor);
} else {
Circle colorCircle = new Circle();
colorCircle.setFill(circleColor);
colorCircle.setManaged(false);
colorCircle.setLayoutX(colorBox.getWidth() / 4);
colorCircle.setLayoutY(colorBox.getHeight() / 2);
colorBox.getChildren().add(colorCircle);
Timeline animateColor = new Timeline(new KeyFrame(Duration.millis(240),
new KeyValue(colorCircle.radiusProperty(),
200,
Interpolator.EASE_BOTH)));
animateColor.setOnFinished((finish) -> {
JFXNodeUtils.updateBackground(colorBox.getBackground(), colorBox, colorCircle.getFill());
colorBox.getChildren().remove(colorCircle);
});
animateColor.play();
}
// update label color
displayNode.setTextFill(circleColor.grayscale().getRed() < 0.5 ? Color.valueOf(
"rgba(255, 255, 255, 0.87)") : Color.valueOf("rgba(0, 0, 0, 0.87)"));
if (colorLabelVisible.get()) {
displayNode.setText(JFXNodeUtils.colorToHex(circleColor));
} else {
displayNode.setText("");
}
}
private void initColor() {
final ColorPicker colorPicker = (ColorPicker) getSkinnable();
Color color = colorPicker.getValue();
Color circleColor = color == null ? Color.WHITE : color;
// update picker box color
colorBox.setBackground(new Background(new BackgroundFill(circleColor, new CornerRadii(3), Insets.EMPTY)));
// update label color
displayNode.setTextFill(circleColor.grayscale().getRed() < 0.5 ? Color.valueOf(
"rgba(255, 255, 255, 0.87)") : Color.valueOf("rgba(0, 0, 0, 0.87)"));
if (colorLabelVisible.get()) {
displayNode.setText(JFXNodeUtils.colorToHex(circleColor));
} else {
displayNode.setText("");
}
}