PreferencesFX

PreferencesFX

JavaFX首选项对话框框架实现简单高效开发

PreferencesFX是一个JavaFX首选项对话框框架,提供简单API用于创建结构化设置界面。支持多种设置类型、搜索过滤和撤销重做等功能,帮助开发者快速构建用户友好的首选项管理界面。该框架支持国际化,并具有丰富的自定义选项,可大幅简化JavaFX应用中首选项功能的开发。

PreferencesFXJavaFX偏好设置用户界面配置管理Github开源项目

JFXCentral

PreferencesFX

Preference dialogs for business applications made easy. Creating preference dialogs in Java has never been this easy!

Maven Central Build Status FOSSA Status

screenshot of created preferences dialog

Table of Contents

Maven

To use this framework as part of your Maven build simply add the following dependency to your pom.xml file:

Java 8

<dependency> <groupId>com.dlsc.preferencesfx</groupId> <artifactId>preferencesfx-core</artifactId> <version>8.6.0</version> </dependency>

Java 11

<dependency> <groupId>com.dlsc.preferencesfx</groupId> <artifactId>preferencesfx-core</artifactId> <version>11.8.0</version> </dependency>

Gradle

To use this framework as part of your gradle build simply add the following to your build.gradle file and use the following dependency definition:

Java 8

dependencies { compile group: 'com.dlsc.preferencesfx', name: 'preferencesfx-core', version: '8.6.0' }

Java 11

dependencies { compile group: 'com.dlsc.preferencesfx', name: 'preferencesfx-core', version: '11.8.0' }

What is PreferencesFX?

Creating preference dialogs in JavaFX is a tedious and very error-prone task. PreferencesFX is a framework which solves this problem. It enables the developer to create preference dialogs with ease and creates well-designed and user-friendly preference dialogs by default.

Advantages

  • Less error-prone
  • Less code needed
  • Easy to learn
  • Easy to understand
  • Easy to use, for developers and users alike

Main Features

  • Simple and understandable API
  • The most important features are noted in the picture and the corresponding table below:

screenshot of created preferences dialog with features

Nr.FeatureDescription
1Search / FilterFilters all categories for a given String. Enables searching for a Setting, Group or Category by name.
2TreeViewShows all categories in a hierarchical structure
3Breadcrumb BarShows the user the previous categories in the hierarchy to the currently displayed category and allows the user to navigate back.
4Undo / Redo ButtonsAllows the user a stepwise undo and redo possibility of his last changes.
5Various pre-defined setting typese.g. Integer, Double, Boolean, String, Lists, Objects
6close/cancel buttonsThe close button just closes the window and leaves the preferences as they are. The cancel button discards all changes which are made during the time the dialog was last opened.
  • | Instant persistance | Any changes to the application are saved instantly.

Documentation

This project uses the asciidoctor plugin to generate the necessary documentation. Run the following maven task:

process-resources

Afterwards, you will find the documentation in the target/generated-docs/ subdirectory.

Structure

A preferences dialog can contain multiple Categories.
Each Category contains one to multiple Groups
Each Group contains one to multiple Settings

For better illustration, the basic concept of writing a preferences dialog is shown below:

PreferencesFx preferencesFx = PreferencesFx.of(SaveClass.class, Category.of("Category Title", Group.of("Group Title", Setting.of("Setting Title", new Property()) ) ) );

Notes:

  • It is also possible to omit the Group and declare all settings in a Category directly. However, in this case all settings will simply be displayed one after another without grouping. If you want more control, use Group.
  • A Group can also be defined without a title. In this case, the individual groups are displayed with more space in between them, to ensure they can be differentiated.
  • A Category can also take a graphic node to be used as an icon as the second argument, e.g. Category.of("Category Title", new ImageView(new Image("file:icon.png")),

Demos

We created several demos to visualize the capabilities of PreferencesFX.
Simply launch preferencesfx-demo/src/com/dlsc/preferencesfx/AppStarter.java and look into the different Tabs.

The following demos are available:

ImportDescription
StandardThe standard demo with a few settings and fully working bindings.
InternationalizedShows how to define preference dialogs in multiple languages, using internationalization.
OneCategoryShows the behavior of the API when only one category is used: The Breadcrumb Bar and TreeView will be omitted from the GUI.
ExtendedA demo, populated with lots of categories, groups and settings without any bindings. Designed to show usage in a big project.

Defining a preferences dialog

Creating a preferences dialog is as simple as calling PreferencesFx.of().

StringProperty stringProperty = new SimpleStringProperty("String"); BooleanProperty booleanProperty = new SimpleBooleanProperty(true); IntegerProperty integerProperty = new SimpleIntegerProperty(12); DoubleProperty doubleProperty = new SimpleDoubleProperty(6.5); PreferencesFx preferencesFx = PreferencesFx.of(AppStarter.class, // Save class (will be used to reference saved values of Settings to) Category.of("Category title 1", Setting.of("Setting title 1", stringProperty), // creates a group automatically Setting.of("Setting title 2", booleanProperty) // which contains both settings ), Category.of("Category title 2") .expand() // Expand the parent category in the tree-view .subCategories( // adds a subcategory to "Category title 2" Category.of("Category title 3", Group.of("Group title 1", Setting.of("Setting title 3", integerProperty) ), Group.of( // group without title Setting.of("Setting title 3", doubleProperty) ) ) ) );

This code snippet results in the following preferences window, containing three categories:

result

To create a Setting, you only need to define a title and a Property. PreferencesFX does the rest.
You can then integrate this Property in your application. Changes of values in the preferences dialog will be persisted instantly, however it's up to you to decide whether you want to persist them instantly in your application as well.

Required arguments

You have a lot of options to influence the behavior and layout of the preferences dialog.
The following parameters are the absolute minimum, needed for the proper functioning of PreferencesFX:

ParameterDescription
AppStarter.classIn the constructor of PreferencesFx a saveClass is required. This class is saved as a key for the saved setting values. Further information is available in the javadoc.
Category descriptionEach Category must have a description. This is required to display its description in the TreeView.
Setting descriptionEach Setting must have a description. It will be displayed on the left of the control, which is used to manipulate the respective Setting.

Note: The value of the each Setting is stored using the Java Preferences API by default.
For testing purposes, to clear the saved preferences of the demo, run the method in the class:

preferencesfx-demo/src/test/java/PreferencesStorageReset.java

Optional arguments

The following parameters are optionally available to further configure the dialog created by PreferencesFX:

MethodClassDescription
.subCategoriesCategorySubcategories allow a Category to have additional subcategories as children. Those are also displayed in the tree.
.expandCategoryAllows to specify if the Category should be expanded in the Tree-View by default.
.descriptionGroupIf you decide not to add the description of a group in the constructor, you can still add it after the creation of the group.
.validateSettingAllows to add a Validator to a setting, to set constraints to the values that can be entered.
.persistApplicationStatePreferencesFxDefines if the Preferences API should save the application states. This includes the state persistence of the dialog window, as well as the values of each Setting.
.persistWindowStatePreferencesFxDefines whether the state of the dialog window (position, size, last selected Category) should be persisted or not. Defaults to false.
.saveSettingsPreferencesFxDefines whether the changed settings in the Preferences window should be saved or not. Defaults to true.
.debugHistoryModePreferencesFxMakes it possible to enable or disable the keycombination to open a debug view of the list of all actions in the history (undo / redo). Pressing Ctrl + Shift + H (Windows) or CMD + Shift + H (Mac) opens a dialog with the undo / redo history, shown in a table. Defaults to false.
.buttonsVisibilityPreferencesFxSets the visibility of the cancel and close buttons in the PreferencesFxDialog. Defaults to true.
.instantPersistentPreferencesFxIf set to true, it will instantly apply any changes that are being made in the PreferencesFxDialog. If set to false, it will only apply changes when the Save / Apply / OK button is pressed. Due to a limitation in FormsFX, undo / redo cannot be used with instant persistence switched off! Defaults to true.
.i18nPreferencesFxSets the translation service of the preferences dialog for internationalization.
.dialogTitlePreferencesFxAllows to specify a custom dialog title.
.dialogIconPreferencesFxAllows to specify a custom dialog icon.

Setting types

The following table shows how to create Settings using the predefined controls and how they look like:

<table> <tr> <th>Syntax</th> <th>Outcome</th> </tr> <tr> <td><pre lang="java"> // Integer IntegerProperty brightness = new SimpleIntegerProperty(50); Setting.of("Brightness", brightness);</pre> </td> <td><img src="./docs/images/settings/integer_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Integer Range IntegerProperty fontSize = new SimpleIntegerProperty(12); Setting.of("Font Size", fontSize, 6, 36);</pre> </td> <td><img src="./docs/images/settings/integerSlider_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Double DoubleProperty scaling = new SimpleDoubleProperty(1); Setting.of("Scaling", scaling);</pre> </td> <td><img src="./docs/images/settings/double_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Double Range DoubleProperty lineSpacing = new SimpleDoubleProperty(1.5); Setting.of("Line Spacing", lineSpacing, 0, 3, 1);</pre> </td> <td><img src="./docs/images/settings/doubleSlider_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Boolean BooleanProperty nightMode = new SimpleBooleanProperty(true); Setting.of("Night Mode", nightMode);</pre> </td> <td><img src="./docs/images/settings/boolean_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // String StringProperty welcomeText = new SimpleStringProperty("Hello World"); Setting.of("Welcome Text", welcomeText);</pre> </td> <td><img src="./docs/images/settings/string_setting.png"></td> </tr> <tr> <td><pre lang="java"> // Combobox, Single Selection, with ObservableList ObservableList<String> resolutionItems = FXCollections.observableArrayList(Arrays.asList( "1024x768", "1280x1024", "1440x900", "1920x1080") ); ObjectProperty<String> resolutionSelection = new SimpleObjectProperty<>("1024x768"); Setting.of("Resolution", resolutionItems, resolutionSelection);</pre> </td> <td><img src="./docs/images/settings/observableList_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Combobox, Single Selection, with ListProperty ListProperty<String> orientationItems = new SimpleListProperty<>( FXCollections.observableArrayList(Arrays.asList("Vertical", "Horizontal")) ); ObjectProperty<String> orientationSelection = new SimpleObjectProperty<>("Vertical"); Setting.of("Orientation", orientationItems, orientationSelection);</pre> </td> <td><img src="./docs/images/settings/listProperty_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Combobox, Multi Selection ListProperty<String> favoritesItems = new SimpleListProperty<>( FXCollections.observableArrayList(Arrays.asList( "eMovie", "Eboda Phot-O-Shop", "Mikesoft Text", "Mikesoft Numbers", "Mikesoft Present", "IntelliG" ) ) ); ListProperty<String> favoritesSelection = new SimpleListProperty<>( FXCollections.observableArrayList(Arrays.asList( "Eboda Phot-O-Shop", "Mikesoft Text")) ); Setting.of("Favorites", favoritesItems, favoritesSelection);</pre> </td> <td><img src="./docs/images/settings/favourites_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Color ObjectProperty<Color> colorProperty = new SimpleObjectProperty<>(Color.PAPAYAWHIP); Setting.of("Font Color", colorProperty);</pre> </td> <td><img src="./docs/images/settings/color_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // FileChooser / DirectoryChooser ObjectProperty<File> fileProperty = new SimpleObjectProperty<>(); Setting.of("File", fileProperty, false); // FileChooser Setting.of("Directory", fileProperty, true); // DirectoryChooser</pre> </td> <td><img src="./docs/images/settings/chooser_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Custom Control IntegerProperty customControlProperty = new SimpleIntegerProperty(42); IntegerField customControl = Field.ofIntegerType(customControlProperty).render( new IntegerSliderControl(0, 42)); Setting.of("Favorite Number", customControl, customControlProperty);</pre> </td> <td><img src="./docs/images/settings/custom_setting.png"/></td> </tr> <tr> <td><pre lang="java"> // Static node Node staticNode = new Label("This can be your very own placeholder!"); Setting.of(staticNode);</pre> </td> <td><img src="./docs/images/settings/static_node.png"/></td> </tr> </table>

Note: By default, PreferencesFX saves the settings under a key which consists of the breadcrumb to the setting, delimited by # signs. If you want to define your own key to be used for saving, use the method setting.customKey("key")

Localisation

All displayed strings can be internationalized. You can use resource bundles to define different locales and use the key instead of the descriptions. Adding i18n support is simply done by calling the method .i18n() at the end when creating the preferences:

private ResourceBundle rbDE = ResourceBundle.getBundle("demo.demo-locale", new Locale("de", "CH")); private ResourceBundle rbEN = ResourceBundle.getBundle("demo.demo-locale", new Locale("en", "UK")); private ResourceBundleService rbs = new ResourceBundleService(rbEN); PreferencesFx.of() .i18n(rbs);

Validation

It is possible to optionally add a Validator to settings. PreferencesFX uses

编辑推荐精选

Keevx

Keevx

AI数字人视频创作平台

Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。

即梦AI

即梦AI

一站式AI创作平台

提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作

扣子-AI办公

扣子-AI办公

AI办公助手,复杂任务高效处理

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

下拉加载更多