How to persiste or save the JavaFX UI state? - java

This has been asked here:
How to persist JavaFX GUI State?
And has the tag javafx-2. But no answers.
Gonna ask again and put in more tags.
I'm looking for something like what was done in JSR 296: Swing Application Framework where the demo application remembers it's window size, location etc. E.g if the user maximized the window, and then closes, opens the program again, the window is maximized.

You can store basic information using the Preferences API.
Depending on the operating system, the preferences are saved in different places (for example in Windows, they are stored in the registry file).
Typical use goes like this:
To store a value:
Preferences prefs = Preferences.userNodeForPackage(MyClass.class);
prefs.putBoolean("isWindowMaximized", true);
To get a value:
Preferences prefs = Preferences.userNodeForPackage(MyClass.class);
boolean value = prefs.getBoolean("isWindowMaximized", false);
You will find more information about this API in this Oracle tutorial.

Related

How to save a state in Shared preferences on Android Java?

In my application after login I have to save state in shared preference to change the app view on a button click. can anyone help me to save state in shared Preferences and on a button click i need to replace the initial state with the new one.
You can't save a layout. You can save a layout id- but I wouldn't suggest it. Resource ids are not stable across builds, so any update would break it. Your best bet is to create an enum with whatever values you wish it to have, save the enum, and later on convert the string back into an enum when you read it in. Then hold a map<enum, resourceId> in code that maps to the correct resource id.

JavaFX: How can I get the character the represents the platform specific SHORTCUT key code?

I am writing a JavaFX application and I have used accelerators to add shortcut keys to the application menus.
Now I am creating the application help and I want to describe the usage of the shortcut keys.
The recommendation for JavaFX accelerators is to use SHORTCUT instead of CONTROL (Windows) and COMMAND (Apple). This works fine and in the menus when running the application on different platforms show the right key combination.
For example, MenuItem Exit I have added the accelerator SHORTCUT_DOWN + X which is displayed as
Ctrl+X under Windows
and
⌘+X under Mac OS
Now I would like to get the explanations (Ctrl+X, ⌘+X) from the system in order to add it to the user help.
Is it possible to ask JavaFX for the presentation of the accelerator in the menu? Or get the presentation of SHORTCUT_DOWN used in the menus?
Thx in advance
Thorsten
According to the documentation, the getDisplayText() method
Returns a string representation of this KeyCombination that is suitable for display in a user interface (for example, beside a menu item).
So all you need is
String acceleratorAsString = menuItem.getAccelerator().getDisplayText();

Saving user settings from GUI

I am currently coding an application, which has an option frame JDialog. This frame contains various checkboxes and text fields that the user can configure.
I want to save the changes the user made to the options.
What is the best way of doing so?
My first thought was implementing it by saving it in a file with the format of e.g.
checkBox1=value;
textArea1="value";
By using the following I could get the field, but I would need to do something like (for the checkbox) myField.isSelected(); which does not work.
Field myField = MyClass.class.getDeclaredField(name);
Thank you in advance.
You can use java.util.prefs.Preferences to store the configuration in your JDialog. This question - Java Preference Manager - discuss about how to create frontend+backend solution by using Preferences (something like JFace org.eclipse.jface.preference)

How to change display settings programically with java?

Is it possible to alter (change/update) display settings (configuration) in Windows XP using Java programming language?
I would like to do something like this:
Display[] displays = WindowsXPSystem.getDisplays(); //get all available displays (monitors). assume there are currently two monitors connected
Display d0 = displays[0]; // the first is 24" and is positioned on the left
d0.setPrimary(true); // and it should be primary, so all new windows open on it.
d0.setSize(new Dimension(1920,1080)); //update screen size (resolution)
d0.setPossition(0,0); //and position it on the left
Display d1 = displays[1]; //second monitor is also present
d1.setSize(new Dimension(1440,768)); // and it's 14.1" laptop's display
d1.setPossition(1920,332); //it's positioned on the right
Any ideas/suggestions/APIs how to update display settings with Java?
I think this is not possible with plain Java. Have a look at this question here:
Detect and Change display resolution permanently using java
as stated there, it's maybe possible to use any native Libraries through JNI (Java Native Interface) which kind of wraps native Libraries. But you will loose your platform independency then.
There is no plain Java solution to your problem. The function is way to specific for a generic implementation.
You could however, if you really need to implement this use a JNI library, that wraps the Windows functionality of adjusting the screen resolution.

Create a setup-frame in Java

My problem is the following: I have a Java application, and I would like to create a setup form on it, so i can declare some variables.
How can it be done? And when the setup form is on focus, it should be disabled to perform actions on the main form.
Thanks for any help.
hectai
Create a JDialog and fill it the controls required to display/change options.
One of the JDialog constructors accepts a boolean argument that tells the dialog to be modal, meaning that the user will be unable to focus on the main form.
If you are using AWT, use Dialog instead of JDialog.
You have wide choice :
1- Java Makefile
2- Setup Maker 1.0
3- I recommended : JSmooth
Use swing. When you press "Save" you store the global variables of your app. If you wanna you can show this form at the start of your app.
As jassuncao posted - you're looking for a JDialog to present this to the user. I'd also suggest using the Java Preferences API over setting variables directly. Preferences has support for saving values between runs, using defaults if no value is provided, etc.

Categories

Resources