Skip to content

Commit

Permalink
Refactored ButtonCell instantiation in PhoneNumberField.
Browse files Browse the repository at this point in the history
Revised the code for instantiating the button cell in PhoneNumberField. Instead of creating a new ButtonCell directly, this update binds the 'buttonCell' property to a newly created object binding. An additional property for the button cell was also added to allow for more flexibility in modification and usage.
  • Loading branch information
dlemmermann committed Feb 13, 2024
1 parent 0cb4210 commit 1d23879
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ protected void layoutChildren(double x, double y, double w, double h) {
comboBox.valueProperty().bindBidirectional(selectedCountryProperty());
comboBox.visibleProperty().bind(showCountryDropdownProperty());
comboBox.managedProperty().bind(showCountryDropdownProperty());

ButtonCell buttonCell = new ButtonCell();
comboBox.setButtonCell(buttonCell);
comboBox.buttonCellProperty().bind(Bindings.createObjectBinding(this::getButtonCell, buttonCellProperty(), valueProperty()));

HBox leftBox = new HBox(comboBox, countryCodePrefixLabel);
leftBox.getStyleClass().add("left-box");
Expand Down Expand Up @@ -569,6 +567,26 @@ public String getUserAgentStylesheet() {
return Objects.requireNonNull(PhoneNumberField.class.getResource("phone-number-field.css")).toExternalForm();
}

private final ObjectProperty<ListCell<Country>> buttonCell = new SimpleObjectProperty<>(this, "buttonCell", new ButtonCell());

public final ListCell<Country> getButtonCell() {
return buttonCell.get();
}

/**
* A property that provided the button cell to be displayed by the ComboBox of the control.
* By default the combo box displays the flag of the currently selected country.
*
* @return a list cell to be used as the button cell
*/
public final ObjectProperty<ListCell<Country>> buttonCellProperty() {
return buttonCell;
}

public final void setButtonCell(ListCell<Country> buttonCell) {
this.buttonCell.set(buttonCell);
}

private class ButtonCell extends ListCell<Country> {

public ButtonCell() {
Expand Down

0 comments on commit 1d23879

Please sign in to comment.