java.lang.Object
org.tentackle.fx.FxUtilities
Utility methods for Fx.
- Author:
- harald
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanapplyBindingOption(FxTextComponent comp, BindingMember member, String option) Applies a single binding option to a text component.protected booleanapplyBindingOption(TableColumnConfiguration columnConfiguration, BindingMember member, String option) Processes an option for a table binding.voidapplyBindingOptions(FxTextComponent comp, BindingMember member, String options) Applies the bindable options to a text component.voidapplyBindingOptions(TableColumnConfiguration columnConfiguration, BindingMember member, String options) Applies the bindable options to a table column.voidapplyStylesheets(javafx.scene.Scene scene) Applies the default stylesheets to a scene.
Invoked for all newly created scenes.voidvoidcloseStageHierarchy(javafx.scene.Node node, javafx.stage.Stage stopStage) Closes the hierarchy of stages from the given node stopping at given stage.<T> voidcollapseAll(Collection<javafx.scene.control.TreeItem<T>> treeItems) Recursively collapses all tree items.<T> voidcollapseAll(javafx.scene.control.TreeItem<T> treeItem) Recursively collapses all tree items.intcomputeScrollToCentered(javafx.scene.control.Skinnable control, int row, int numRow) Computes the row number to scrollTo in order to position the given row in the middle of the viewport.createInteractiveError(ValidationResult validationResult, NavigableSet<ValidationMapper> validationMappers, Binder binder) Creates an interactive error from a validation result.createInteractiveErrors(List<ValidationResult> validationResults, NavigableSet<ValidationMapper> validationMappers, Binder binder) Creates interactive errors from validation results.javafx.geometry.Point2DdetermineAlignedStageLocation(javafx.stage.Stage stage, javafx.geometry.Point2D location) Calculates the location of a stage so that it is completely visible on the screen, using a "free" spot.javafx.geometry.Point2DdetermineCenteredLocation(javafx.stage.Window window) Calculates the location of a window so that it will be centered on the screen.javafx.geometry.Point2DdeterminePreferredStageLocation(javafx.stage.Stage stage) Calculates the position of a stage on the screen so that it is being display in an optimal manner.dumpComponentHierarchy(javafx.scene.Node node) Dumps the component hierarchy.voidEdits/opens a given file via the Desktop utilities.
The corresponding application will be chosen according to the mime/file type.<T> voidexpandAll(javafx.scene.control.TreeItem<T> treeItem) Recursively expands all tree items while checking for recursion loops.
The default implementation in TreeViewBehavior and TreeTableViewBehavior is not only in a private API, but cannot be accessed from the applications at all.voidfilterKeys(javafx.scene.control.Control control) Filters certain keys for special features.voidfocusGained(FxComponent component) Performs all necessary operations on an FxComponent when it gained the focus.voidfocusLost(FxComponent component) Performs all necessary operations on an FxComponent when it lost the focus.javafx.collections.ObservableList<javafx.stage.Stage>Delivers a lis of all showing stages.getBindingOption(String options, String key) Gets a binding option from an options string.Gets the separator string for CSV exports.javafx.scene.CursorgetDefaultCursor(javafx.scene.Node node) Gets the default cursor for the given node.intgetDefaultTextAreaColumns(FxTextArea textArea) Gets the default column width for text areas if not set by the binding or the model's COLS= option.Gets the help URL prefix.static FxUtilitiesThe singleton.longGets the timeout for the prefix selection in dropdown lists such as in combo boxes.javafx.stage.StagegetStage(javafx.scene.Node node) Gets the stage for a node.javafx.scene.control.skin.VirtualFlowgetVirtualFlow(javafx.scene.control.Skinnable control) Gets the virtual flow for a given control.javafx.scene.CursorgetWaitCursor(javafx.scene.Node node) Gets the wait cursor for the given node.javafx.stage.WindowGets the window of an owner object.
The owner can be a window, a scene or a node.
ThrowsFxRuntimeExceptionif the window cannot be determined.booleanReturns whether to enable dark mode.booleanisLenientMoneyInput(FxTextComponent component) Returns whether decimal separator should be inserted automatically if missing in money input.
This is usually a global setting for the whole application, but it can be filtered according to the component (parent component, etc...).booleanisModal(javafx.stage.Stage stage) Returns whether the stage is modal.<T> booleanisValueInParentPath(javafx.scene.control.TreeItem<T> treeItem) Checks whether one of the parents already contains the item's value.voidprint(javafx.scene.Node node) Prints a node.
The user selects the printer, and the node is scaled down if too large for the paper.voidregisterWindowEventFilters(javafx.stage.Window window) Registers event filters for windows.voidremapKeys(javafx.scene.control.Control control) Remaps certain keys.voidresizeColumnsToFitContent(javafx.scene.control.TableView<?> table) Resizes the width of all columns of a table to fit the displayed content.
Unlike the "double-click on column separator"-feature this method computes the column widths according to the content currently displayed.voidresizeColumnsToFitContent(javafx.scene.control.TreeTableView<?> table) Resizes the width of all columns of a tree table to fit the displayed content.
Unlike the "double-click on column separator"-feature this method computes the column widths according to the content currently displayed.voidRuns a runnable in a separate thread.
Avoids blocking the FX-application thread.
If the execution fails, an error dialog is shown.voidrunAndWait(Runnable runnable) Runs a command on the FX application thread and waits for its execution to finish.voidsetDarkMode(boolean darkMode) Enables dark mode.voidsetHelpURL(String helpURL) Sets the help URL prefix.voidsetupFocusHandling(javafx.scene.control.Control control) Adds focus handling to sync with model.voidshowErrorDialog(Object owner, String message, Throwable t, Runnable onClose) Shows an error dialog.javafx.stage.PopupWindowshowErrorPopup(ErrorPopupSupported component) Shows an error popup for a component.voidOpens the online help for a given component.voidshowInfoDialog(Object owner, String message, Runnable onClose) Shows an info dialog.javafx.stage.PopupWindowshowInfoPopup(InfoPopupSupported component) Shows an info popup for a component.voidshowNotification(Object owner, javafx.stage.Popup popup, javafx.scene.Parent notification, Runnable onClose) Shows a notification popup.
The notification is centered above the owner window.voidshowQuestionDialog(Object owner, String message, boolean defaultYes, Consumer<Boolean> answer) Shows a question dialog.
To avoid blocking the event queue (seeDialog.showAndWait()), the implementation is not based onAlertand the method returns immediately.booleanshowValidationResults(Object owner, ValidationFailedException ex, NavigableSet<ValidationMapper> validationMappers, Binder binder) Shows the validation warnings and errors in a dialog and marks theFxControls related to those errors.
Errors and warnings are shown in error or info dialogs.voidshowWarningDialog(Object owner, String message, Runnable onClose) Shows a warning dialog.voidTerminates the FX client.
The default implementation invokesPlatform.exit().
-
Constructor Details
-
FxUtilities
public FxUtilities()Creates the FX utilities.
-
-
Method Details
-
getInstance
The singleton.- Returns:
- the singleton
-
isDarkMode
public boolean isDarkMode()Returns whether to enable dark mode.- Returns:
- true to enable dark mode
-
setDarkMode
public void setDarkMode(boolean darkMode) Enables dark mode.- Parameters:
darkMode- true to enable dark theme, false for light theme (default)
-
applyStylesheets
public void applyStylesheets(javafx.scene.Scene scene) Applies the default stylesheets to a scene.
Invoked for all newly created scenes.- Parameters:
scene- the scene
-
getDefaultCursor
public javafx.scene.Cursor getDefaultCursor(javafx.scene.Node node) Gets the default cursor for the given node.- Parameters:
node- the node- Returns:
- the default cursor, never null
-
getWaitCursor
public javafx.scene.Cursor getWaitCursor(javafx.scene.Node node) Gets the wait cursor for the given node.- Parameters:
node- the node- Returns:
- the wait cursor, never null
-
isLenientMoneyInput
Returns whether decimal separator should be inserted automatically if missing in money input.
This is usually a global setting for the whole application, but it can be filtered according to the component (parent component, etc...).- Parameters:
component- the input component- Returns:
- true if insert separator according to scale if missing
-
getDefaultTextAreaColumns
Gets the default column width for text areas if not set by the binding or the model's COLS= option.- Parameters:
textArea- the text area component- Returns:
- the column width (default is 30)
-
getPrefixSelectionTimeout
public long getPrefixSelectionTimeout()Gets the timeout for the prefix selection in dropdown lists such as in combo boxes.- Returns:
- the timeout in milliseconds (default is 750)
-
setHelpURL
Sets the help URL prefix.- Parameters:
helpURL- the URL prefix
-
getHelpURL
Gets the help URL prefix.- Returns:
- the URL prefix
-
showHelp
Opens the online help for a given component.- Parameters:
owner- the owner window or node- See Also:
-
terminate
public void terminate()Terminates the FX client.
The default implementation invokesPlatform.exit(). -
browse
- Parameters:
uri- the URI to be displayed in the user default browser
-
edit
Edits/opens a given file via the Desktop utilities.
The corresponding application will be chosen according to the mime/file type.- Parameters:
owner- the owner window or nodefile- the file
-
getStage
public javafx.stage.Stage getStage(javafx.scene.Node node) Gets the stage for a node.- Parameters:
node- the node- Returns:
- the stage, null if node does not belong to a scene or scene does not belong to a stage.
-
getWindow
Gets the window of an owner object.
The owner can be a window, a scene or a node.
ThrowsFxRuntimeExceptionif the window cannot be determined.- Parameters:
owner- the owner object- Returns:
- the window
-
isModal
public boolean isModal(javafx.stage.Stage stage) Returns whether the stage is modal.- Parameters:
stage- the stage- Returns:
- true if effectively modal
-
closeStageHierarchy
public void closeStageHierarchy(javafx.scene.Node node, javafx.stage.Stage stopStage) Closes the hierarchy of stages from the given node stopping at given stage.- Parameters:
node- the nodestopStage- the stop stage, null to close the whole application
-
dumpComponentHierarchy
Dumps the component hierarchy.- Parameters:
node- the node- Returns:
- the formatted string
-
runAndWait
Runs a command on the FX application thread and waits for its execution to finish.- Parameters:
runnable- the Runnable whose run method will be executed on the FX application thread- Throws:
FxRuntimeException- if the calling thread is the FX application thread- See Also:
-
Platform.runLater(java.lang.Runnable)
-
run
Runs a runnable in a separate thread.
Avoids blocking the FX-application thread.
If the execution fails, an error dialog is shown.- Parameters:
owner- the owner window or noderunnable- the runnable to execute in background
-
showNotification
public void showNotification(Object owner, javafx.stage.Popup popup, javafx.scene.Parent notification, Runnable onClose) Shows a notification popup.
The notification is centered above the owner window.- Parameters:
owner- the owner window or nodepopup- the popupnotification- the notification nodeonClose- the optional runner to execute when notification is closed/hidden
-
showInfoDialog
Shows an info dialog.- Parameters:
owner- the owner window or nodemessage- the messageonClose- optional runnable invoked when dialog is closed
-
showWarningDialog
Shows a warning dialog.- Parameters:
owner- the owner window or nodemessage- the messageonClose- optional runnable invoked when dialog is closed
-
showQuestionDialog
public void showQuestionDialog(Object owner, String message, boolean defaultYes, Consumer<Boolean> answer) Shows a question dialog.
To avoid blocking the event queue (seeDialog.showAndWait()), the implementation is not based onAlertand the method returns immediately. The result is then provided via aConsumer.
As a convenience, typing the key "0" is interpreted as "no" and "1" as "yes".Note: if you need to wait for a decision of the user (i.e. no async use), you can do that from another than the FX-thread by using a
BlockingHolderlike so:BlockingHolder<Boolean> answer = new BlockingHolder<>(); Platform.runLater(() -> Fx.question(..., answer); if (answer.get()) { ... } else { ... }- Parameters:
owner- the owner window or nodemessage- the messagedefaultYes- true if yes is the default buttonanswer- the user's answer withBoolean.TRUEfor YES orBoolean.FALSEfor NO, never null
-
showErrorDialog
Shows an error dialog.- Parameters:
owner- the owner window or nodemessage- the messaget- optional throwableonClose- optional runnable invoked when dialog is closed
-
showErrorPopup
Shows an error popup for a component.- Parameters:
component- the component- Returns:
- the popup, null if no errormessage in component
-
showInfoPopup
Shows an info popup for a component.- Parameters:
component- the component- Returns:
- the popup, null if no info-message in component
-
setupFocusHandling
public void setupFocusHandling(javafx.scene.control.Control control) Adds focus handling to sync with model.- Parameters:
control- the fx control
-
focusGained
Performs all necessary operations on an FxComponent when it gained the focus.- Parameters:
component- the fx component
-
focusLost
Performs all necessary operations on an FxComponent when it lost the focus.- Parameters:
component- the fx component
-
remapKeys
public void remapKeys(javafx.scene.control.Control control) Remaps certain keys.- Parameters:
control- the fx control
-
filterKeys
public void filterKeys(javafx.scene.control.Control control) Filters certain keys for special features.- Parameters:
control- the fx control
-
registerWindowEventFilters
public void registerWindowEventFilters(javafx.stage.Window window) Registers event filters for windows.- Parameters:
window- the window
-
expandAll
public <T> void expandAll(javafx.scene.control.TreeItem<T> treeItem) Recursively expands all tree items while checking for recursion loops.
The default implementation in TreeViewBehavior and TreeTableViewBehavior is not only in a private API, but cannot be accessed from the applications at all. The default implementation is dangerous since it does not check for recursion loops. We replace it by mapping the event to this utility method.- Type Parameters:
T- the item's value type- Parameters:
treeItem- the item to start expansion
-
isValueInParentPath
public <T> boolean isValueInParentPath(javafx.scene.control.TreeItem<T> treeItem) Checks whether one of the parents already contains the item's value.- Type Parameters:
T- the value's type- Parameters:
treeItem- the tree item to check- Returns:
- true if value is in path
-
collapseAll
public <T> void collapseAll(javafx.scene.control.TreeItem<T> treeItem) Recursively collapses all tree items.- Type Parameters:
T- the item's value type- Parameters:
treeItem- the item to start
-
collapseAll
Recursively collapses all tree items.- Type Parameters:
T- the item's value type- Parameters:
treeItems- the list of items to collapse- See Also:
-
print
public void print(javafx.scene.Node node) Prints a node.
The user selects the printer, and the node is scaled down if too large for the paper.- Parameters:
node- the node to print
-
getBindingOption
Gets a binding option from an options string.- Parameters:
options- the binding options stringkey- the option key- Returns:
- the option, null if no such option
-
applyBindingOptions
Applies the bindable options to a text component.- Parameters:
comp- the componentmember- the binding memberoptions- the options from the @Bindableannotation
-
applyBindingOptions
public void applyBindingOptions(TableColumnConfiguration columnConfiguration, BindingMember member, String options) Applies the bindable options to a table column.- Parameters:
columnConfiguration- the column configurationmember- the binding memberoptions- the options from the @Bindableannotation
-
applyBindingOption
Applies a single binding option to a text component.- Parameters:
comp- the componentmember- the binding memberoption- the option- Returns:
- true if option known and applied, false if unknown option
-
applyBindingOption
protected boolean applyBindingOption(TableColumnConfiguration columnConfiguration, BindingMember member, String option) Processes an option for a table binding.- Parameters:
columnConfiguration- the column configmember- the binding memberoption- the option- Returns:
- true if option known and processed, false if unknown option
-
resizeColumnsToFitContent
public void resizeColumnsToFitContent(javafx.scene.control.TableView<?> table) Resizes the width of all columns of a table to fit the displayed content.
Unlike the "double-click on column separator"-feature this method computes the column widths according to the content currently displayed. For tables with a large number of rows this is much more efficient than scanning the whole table.- Parameters:
table- the table view
-
resizeColumnsToFitContent
public void resizeColumnsToFitContent(javafx.scene.control.TreeTableView<?> table) Resizes the width of all columns of a tree table to fit the displayed content.
Unlike the "double-click on column separator"-feature this method computes the column widths according to the content currently displayed. For tables with a large number of rows this is much more efficient than scanning the whole table.- Parameters:
table- the tree table view
-
getAllShowingStages
public javafx.collections.ObservableList<javafx.stage.Stage> getAllShowingStages()Delivers a lis of all showing stages.- Returns:
- the stages
-
determineCenteredLocation
public javafx.geometry.Point2D determineCenteredLocation(javafx.stage.Window window) Calculates the location of a window so that it will be centered on the screen.- Parameters:
window- the window- Returns:
- the location (top left corner)
-
determinePreferredStageLocation
public javafx.geometry.Point2D determinePreferredStageLocation(javafx.stage.Stage stage) Calculates the position of a stage on the screen so that it is being display in an optimal manner.- Parameters:
stage- the stage to be positioned on the screen- Returns:
- the location
-
determineAlignedStageLocation
public javafx.geometry.Point2D determineAlignedStageLocation(javafx.stage.Stage stage, javafx.geometry.Point2D location) Calculates the location of a stage so that it is completely visible on the screen, using a "free" spot.- Parameters:
stage- the current stagelocation- the desired (not necessarily current!) location- Returns:
- the aligned location
-
getVirtualFlow
public javafx.scene.control.skin.VirtualFlow getVirtualFlow(javafx.scene.control.Skinnable control) Gets the virtual flow for a given control.- Parameters:
control- the control- Returns:
- the virtual flow, null if no virtual flow associated to the control
-
computeScrollToCentered
public int computeScrollToCentered(javafx.scene.control.Skinnable control, int row, int numRow) Computes the row number to scrollTo in order to position the given row in the middle of the viewport.- Parameters:
control- the control, usually a table- or tree-viewrow- the row to position in the middle of the viewport, if possiblenumRow- the total number of rows- Returns:
- the row number to pass to the scrollTo method
-
getCsvSeparator
Gets the separator string for CSV exports.- Returns:
- the separator, default is
";"
-