wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Swing, awareness about unsaved data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing, awareness about unsaved data" Watch "Swing, awareness about unsaved data" New topic
Author

Swing, awareness about unsaved data

Gerald Frischl
Greenhorn

Joined: Jul 23, 2007
Posts: 4
Hi,

I am working on a Swing application for data maintenance (Java 6).
There is a JFrame with several JTextFields.
If the user is editing a record, there is UNSAVED data until she/he presses the Save-Button.

The application should always be aware if there is unsaved data. How can this be done?

Best regards
Gerald
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41521
    
  53
Welcome to JavaRanch.

You could keep a boolean around that indicates whether there is unsaved data. Initially it would be false, but would be set to true in a key listener attached to each field.

(The logic in the key listener could be a bit more elaborate, like detecting if the new value is actually different from the last saved value. E.g., typing a key, and then hitting delete, should not count as a change in this context.)

Once the data is saved, the boolean is reset to false.


Ping & DNS - my free Android networking tools app
Gerald Frischl
Greenhorn

Joined: Jul 23, 2007
Posts: 4
Thank you, Ulf!

A possible solution is: after each keystroke I compare the new
content of JTextField with the stored old string. If there is a difference
I set the "changed"-flag.

Does this make sense or is there a better way?

Best regards
Gerald
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41521
    
  53
Well, if you really want to take the old, saved value into account, then you'd need a "changed" flag for each textfield. The overall "changed" status would then be the logical OR of all individual flags.

If you have just a single flag, then you can't take individual field's changed/unchanged status into account.

Consider these two cases: 1) field A changes, then field B changes, then A changes back to its original value, and 2) field A changes, then A changes back to its original value.
The warning "you have unsaved changes" (or whatever the code is going to do) should be shown for case 1, but not for case 2. But these cases can't be distinguished with just a single flag.
Gerald Frischl
Greenhorn

Joined: Jul 23, 2007
Posts: 4
Thank you again!

I'll think it over.

But isn't there a standard solution to this (common) task?

Best regards
Gerald
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this might be one way, flag is set on any keystroke - modify to suit.
e.g.
1)
type some text, click save, exit - exits without warning
2)
type some text, click save, ctrl-c to copy the text, exit - warning displays

Gerald Frischl
Greenhorn

Joined: Jul 23, 2007
Posts: 4
Hi Michael,

you've answered my next question:
How can I prevent data loss when closing the application?

Thanks a lot!
Gerald
Anton Kuzmin
Greenhorn

Joined: Feb 22, 2012
Posts: 19

This code won't work if user is changing state with a mouse.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> This code won't work if user is changing state with a mouse.

if you're talking about adding a mouseListener with right-click to open a popup
with 'paste' code, then, yes, it probably won't work.

also won't work if there's a tsunami or nuclear exlosion.
Anton Kuzmin
Greenhorn

Joined: Feb 22, 2012
Posts: 19

There are also controls that are usually used with a mouse. JSpinner, for example. So this code isn't very good choice for form-based frames and dialogs.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

Anton Kuzmin wrote:There are also controls that are usually used with a mouse. JSpinner, for example. So this code isn't very good choice for form-based frames and dialogs.


The original post stated
Gerald Frischl wrote:There is a JFrame with several JTextFields.

The responses were geared to that statement. If you have a question about saving state involving a GUI with JSpinners, I suggest you start a new thread.


luck, db
There are no new questions, but there may be new answers.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> There are also controls that are usually used with a mouse. JSpinner, for example....

no idea what you're on about, or why you'd even bother resurrecting a 5-year-old thread, but different components have different listeners.

in this thread textFields = keyListeners (as one option).

if the topic had been about spinners and the solution was about using changeListeners, would you have posted something like this?
"that's no good because it won't work with textfields"

I suspect you're just trolling a bit.

</end of discussion>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Swing, awareness about unsaved data