• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing, awareness about unsaved data

 
Gerald Frischl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gerald Frischl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code won't work if user is changing state with a mouse.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 19
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic