Results 1 to 11 of 11
  1. #1
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Question how to code dynamic exit function for a JButton

    for a button i have written dynamic code for popping up new frame asking user whether "he wants to exit the application?" with two button's YES and NO.

    private void ExitWindowActionPerformed(java.awt.event.ActionEve nt evt) {

    System.exit(0);
    /*
    JFrame ExitFrame = new JFrame(" Exit Application");
    ExitFrame.setVisible(true);

    ExitFrame.setSize(400,100);
    JLabel ExitLabel =new JLabel ("Are you sure you want to exit this Seamless Access");
    JPanel ExitPanel =new JPanel();
    ExitFrame.add(ExitPanel);
    ExitPanel.add(ExitLabel);
    /* expecting some code here for dynamic exitting the application*/
    JButton ExitButtonYes = new JButton("Yes");
    ExitPanel.add(ExitButtonYes);
    /*expecting some code here for dynamic cancel the exit window and continue with the software*/

    JButton ExitButtonNo = new JButton("No");
    ExitPanel.add(ExitButtonNo);
    */

    }

    How to write dynamic exit function below the line "ExitPanel.add(ExitLabel);" and also "ExitPanel.add(ExitButtonNo);"

    please help me out

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

  3. #3
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default how to code dynamic exit function for a JButton

    Thanks for Replying

    Dynamic means it is done when the particular code is made to run.

    like in my code i have written for pop-up as shown below

    Are sure you want to exit application?

    Yes No


    if Yes is pressed my application should exit and if No it should not

    so when pressed Yes it shld call tat code for exit dynamically, i feel
    anything wrong i told correct me. i am still new to java.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    1) In your main frame you need to add the following line of code. This will prevent the frame from closing automatically and will allow you to close the frame or leave it open depending on the user response.

    2) You need to add a WindowListener to the frame and handle the windowClosing() event. Darryl gave you the link to the Swing tutorial that explains how to write listeners

    3) In the listener code you will display a JOptionPane (not a JFrame) asking the user what they want your to do. Again, Darryl gave you the link on using dialogs.

    4) You respond to the selection of the user either by doing nothing or by invoking dispose() on the frame.

  5. #5
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default how to code dynamic exit function for a JButton

    Hi Camickr,

    Thanks for replying

    As you said i have tired doing WindowListener, but i got an error saying Illegal start of expression, because it was declared within an ActionPerformed event,
    and can you please help me in filling the code in between those lines which i have posted i have been trying from past 2 days and have done enough googling to solve it, but was unsuccessful.

    thanks

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Stop wasting time trying to find ready made code on the net and go through the Swing tutorials systematically.

    Believe me, it's much more fun writing code when you know what you are doing.

    And I still don't get what's 'dynamic' about code execution doing what it's supposed to do.

    db

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    i got an error saying Illegal start of expression, because it was declared within an ActionPerformed event
    Better start here: Trail: Learning the Java Language (The Java™ Tutorials)

    db

  8. #8
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default how to code dynamic exit function for a JButton

    ]Hi

    Thanks a lot DarrylBurke and camickr, thanks for the tutorials

    I finally solved it using JOpyionPane.

    Regards
    Sandeep

  9. #9
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    I was going to mention JOptionPane. All you need to do is :

    Java Code:
    int response = JOptionPane.showConfirmDialog(null, "Exit program?", "Confirm",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (response == JOptionPane.NO_OPTION) 
        {
              //do nothing    
        } 
        else if (response == JOptionPane.YES_OPTION) 
        {
              System.exit(0)
        }

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Quote Originally Posted by d3n1s View Post
    Java Code:
    if (response == JOptionPane.NO_OPTION) 
        {
              //do nothing    
        } 
        else if (response == JOptionPane.YES_OPTION) 
        {
              System.exit(0)
        }
    Which is equivalent to
    Java Code:
    if (response == JOptionPane.YES_OPTION) 
        {
              System.exit(0)
        }
    Why write empty blocks for nothing?

    db

  11. #11
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    LOL you're right but maybe he wants to do something else when the user hits no or something, i was just providing the basic structure.

Similar Threads

  1. exit code for JApplet
    By tnixon22 in forum New To Java
    Replies: 1
    Last Post: 02-25-2011, 04:56 AM
  2. JVM terminated.Exit code=13
    By Hussain Ali in forum Eclipse
    Replies: 2
    Last Post: 11-02-2010, 10:02 AM
  3. shell exit code
    By mac in forum New To Java
    Replies: 2
    Last Post: 05-17-2010, 11:58 PM
  4. JVM terminated Exit code=1
    By oregon in forum Eclipse
    Replies: 3
    Last Post: 09-29-2008, 09:49 PM
  5. help with System.exit (1) function call
    By katie in forum Advanced Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •