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!
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
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.
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!
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.