Swing dialog box management system #1046
Unanswered
Genie23
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I want to share with you today my system to facilitate the management of Swing dialogs in a Compose Desktop application, which is meant to be easy to use.
Installation
First step, create a Kotlin file in which you will put this content :
Initialization of the dialogs
Then, in your application, you will have to call the following method which will import in the application tree the @composable objects as required :
SystemDialogs()
The import must be done outside of any control structure so that the dialogs are always available.
Usage
When you need a dialog box managed by my system, from a callback like when you click on a button or in a menu for example, you just have to call one of the following methods with the right parameters:
openAbout()
: displays the dialog box defined in the DialogAbout function. You can customize this dialog box by modifying the DialogAbout function at the beginning of the script;openMessage(title:String, message:String, type:Int = JOptionPane.INFORMATION_MESSAGE)
: displays a message dialog with the title and message passed as parameters. In addition, you can specify the type of dialog box you want by passing one of the following values to the type parameter:JOptionPane.INFORMATION_MESSAGE
: information dialog (default value);openInput(message:String) { input:String -> ... }
: displays a simple input prompt with the message passed as a parameter and then executes the action by passing it the input value ;openConfirm(title:String, message:String, type:Int = JOptionPane.INFORMATION_MESSAGE) { confirmed:Boolean -> ... }
: displays a confirmation dialog box with the title and the message passed as parameters. The type parameter, which is identical to that of the openMessage function, allows the user to specify the type of dialog box desired. Then execute the action by passing a boolean which is true if the user has clicked on YES, false otherwise;openFileChooser(title:String = "Choose a file", mode:Dialog.Mode = Dialog.Mode.LOAD, extensions:List<FileNameExtensionFilter> = listOf()) { files:List<File> -> ...}
: displays a file choice dialog box with the title you want. The way the dialog box is opened defines its behavior. It can be eitherDialog.Mode.LOAD
, in which case you will be able to select one or more files, and only the existing files (readable) will be returned to the list passed to the action, orDialog.Mode.SAVE
, in which case the file may not exist, and in case it already exists (readable always), a confirmation dialog box will open asking the user if he wants to replace this file. The extensions parameter is a list of objects of type FileNameExtensionFilter (whose constructor first takes a string which will be the description of the filter in the dialog box, then all the file extensions without initial . that must be accepted by the filter). If it is not specified, then no filter will be applied. If the open mode is SAVE, then the name of the file to be saved will be parsed by the filter, and if the chosen file does not have an extension that is accepted by the current filter, the first extension of the filter will be added to the name of the file which will then be passed to the action (still in the form of a list, in this case an item);Example
An example of a dialog box for saving a Word/ODT file:
And I was able to place this code in the onClick callback function of a button.
Beta Was this translation helpful? Give feedback.
All reactions