tags:

views:

53

answers:

3

I am new to Java Swing and I am creating a window which displays a list of items retrieved from an XML file that can be manipulated by the user. The window should have a Cancel and a Save functionality implemented with buttons. While the Save functionality is straightforward (just close the window) I don't know how to implement the Cancel functionality. Does exist an "undo" function? Does anyone know how?

+1  A: 

Can't you make a loop that iterates throught all the fields; changing all values in the fields to null for cancel and then close the window?

Bilzac
Maybe u can create a boolean value that changes depending on whether the user presses cancel or save. So the back-end part of your code knows exactly what button u just pressed.
Bilzac
@Bilzac +1 for separating business logic from the UI
glowcoder
@glowcoder: Actually that couples the two together even closer. "Cancel or save" is inherently a GUI concept; by introducing that into the business logic you couple it to a GUI. A command from a command line to do the same thing, for example, would not have the concept of save/cancel.
Mark Peters
@Mark Hmm I guess I interpreted it differently. I took to mean there's back-end code for cancel or save that gets called from the button handler as opposed to doing it in the handler itself. For the record, I think the most straightforward answer is your original comment.
glowcoder
@glowcoder Thanks for the +1, too bad someone gave me a -1 :(. @Mark, it was a rough idea. Though i still think it can be implemented. The only difference between cancel and save is that save actually updates the content while cancel doesnt. The business logic doesnt necessarily have to get attached to the GUI. You can incase your previous method in another one which is designed for the gui (with the extra parameter). Which doesnt necessarily couple it.
Bilzac
@Bilzac: I think glow's +1 was to your comment, which still stands. As for the suggestion, I'd have to see it to comment on it further. I suppose there might be a clean way it could work. I was objecting most severly to your original statement *"so the back-end part of your code knows exactly what button u just pressed"*. Your back-end shouldn't even know there are buttons!
Mark Peters
@Mark, sorry for the misunderstanding but yeah ure right about that! :)
Bilzac
+1  A: 

it depends on whether you have a modal or modeless window/dialog for a modal/dialog window you should ask the other way around: first store you values when the user clicks save/OK.

for a modeless window you could hold a copy of your data thats editable in the window in your window and save when the user clicks save and do nothing when the user clicks Cancel

Oops
+1  A: 

If it is about to restore the internal (changed) datas tructure for subsequent processing, you could either duplicate the data before editing, or simply reread the XML file. If you don't need the data any longer simply do nothing on cancel (but closing the window).

stacker