2

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?

Khalid Amin
  • 872
  • 3
  • 12
  • 26

1 Answers1

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