How can I restrict the user from going away from the current tab. I need to show a confirmation message to save/discard changes before leaving the current tab. How can I implement this functionality in a JTabbedPane?
Asked
Active
Viewed 407 times
2
-
1I think you can put the confirmation message in void stateChanged(ChangeEvent e) – Eng.Fouad May 10 '11 at 20:30
1 Answers
4
You could override the setSelectedIndex in the JTabbedPane object, as this is called before the change takes place. If you do this, remember to call super.setSelectedIndex after you displayed your confirmation.
Alternatively, as Eng.Fouad states, you could add a ChangeListener and display the confirmation message there, but this happens after the tab has been changed, so your confirmation message would display after the new tab has been displayed on screen: http://www.java2s.com/Tutorial/Java/0240__Swing/ListeningforSelectedTabChanges.htm
The second option would be easier to implement if you can live with the fact that the new tab will already be displayed when the confirmation pops up.

Riaan Cornelius
- 1,933
- 1
- 14
- 17