• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTabbedPane-How to tell if changes were made before switching tabs  RSS feed

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTabbedPane with 5 different tabs on it. There are numerous fields on these 5 different tabs such as JTextFields, JComboBoxes, JLabels, and a custom class (for address).
Each of these tabs have a save button on them. if the user tries to click on a different tab and has made changes to the current tab, but didn't press save, I want to pop up a message asking them if the want to save changes before switching to the next tab. How would I go about doing this?
Compare values to the database, or listen for changes? If listen for changes, how do I do that with the multiple components?
Thanks for any input!
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
you could have a data model for each tab, that stores the values. whenever the values get changed, the model sets a flag that it has entered the modified state. when the user want to switch tabs, you ask the model whether it is modified. if so you popup the dialog. if the values get saved the model has to be set to unmodified.
I would not compare the values in the database directly this would slow down the program.
Chantal
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a great idea! However, I have never done this before and am at a loss. I looked in the JTabbedPane API but didn't see anything that helped me a whole lot. Maybe I'm looking for the wrong thing. Could someone give me a nudge in the right direction to get me started? For example, maybe a sample from somewhere?
Thank you so much.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use listeners. A single document listener(text fields) and item listener (combos) should do it. You have tabs, do you also have to worry about menu changes?

Just make sure to set it to false if you fill the fields in when entering. The listener will set it true when you fill the fields.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am implementing the document listeners for text fields and combo boxes. Thanks for the suggestions.
However, I have another problem. When a ChangeListener event is fired, I need to get the index of the tab that I am coming off of. If I do a getSelectedIndex() within the stateChanged method, it gives me index of the tab that it is going to switch to. Do you have any suggestions on how to get this information?
Thanks again!
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why you don't remember the last selected Tab? For the first time, it is up to you to set a Tab. So maybe the last Tab is -1. Then when your event is fired, you can do whatever you want with the information of the last tab. After this, you set the last tab to the current one. This is the most simple thing, which comes into my mind.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!