code for a dialogue box to exit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugard
    New Member
    • Aug 2007
    • 50

    #1

    code for a dialogue box to exit

    I am trying to do this thing. When someone presses the exit button, a new window will be displayed asking the user whether he is sure to exit also 2 buttons will be displayed. One displaying yes and a one displaying no. If yes pressed the application closes if not, the user will be returned to the application. Though, I dont have an idea what code i need to place under each ActionPerformed for the button Yes and No. Can please someone help me how to do this?

    thanks very much
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by sugard
    I am trying to do this thing. When someone presses the exit button, a new window will be displayed asking the user whether he is sure to exit also 2 buttons will be displayed. One displaying yes and a one displaying no. If yes pressed the application closes if not, the user will be returned to the application. Though, I dont have an idea what code i need to place under each ActionPerformed for the button Yes and No. Can please someone help me how to do this?

    thanks very much
    To display a "Are you sure you want to exit? yes/no" dialog, you can use JOptionPane. This is very simple -- one line of code.

    JOptionPane Tutorial
    JOptionPane API

    To react when the user tries to close the main frame, use a WindowListener:

    Window Listener Tutorial

    In this case, be sure to set the default behaviour of the window to do nothing on close -- you will be manually coding it:

    [CODE=Java]mainWindow.setD efaultCloseOper ation(WindowCon stants.DO_NOTHI NG_ON_CLOSE);[/CODE]

    Comment

    Working...