Results 1 to 10 of 10
  1. #1
    NorrinGalan is offline Member
    Join Date
    Feb 2014
    Posts
    12
    Rep Power
    0

    Default Exit Java Applet

    I am finishing work on an applet. I have an event handling for a button and it is supposed to close the applet when the button is clicked. This works fine in my IDE, which is Eclipse. However, when I try to close the applet in the web browser in its URL, the applet will not close.

    I tried with System.exit(0), and System.exit(1), but none will work.

    I also tried applet.getAppletContext().showDocument(appletClose URL), but for some reason I cannot get it to work.

    If my URL is javaApplet.com, would this be correct:

    this.getAppletContext().showDocument(javaApplet.co m);

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default Re: Exit Java Applet

    You could draw a cover over the applet's window with a message that it is closed.

    Are there any error messages from the showDocument() call?
    Last edited by Norm; 05-14-2014 at 02:01 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    NorrinGalan is offline Member
    Join Date
    Feb 2014
    Posts
    12
    Rep Power
    0

    Default Re: Exit Java Applet

    It states that getAppletContext() is undefined.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default Re: Exit Java Applet

    Have you looked at the API doc to see what class that method is defined in?
    Is the code calling getAppletContext() using the correct class reference? What class does this refer to?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    NorrinGalan is offline Member
    Join Date
    Feb 2014
    Posts
    12
    Rep Power
    0

    Default Re: Exit Java Applet

    I think that the class is Applet.

    I put the code in an action listener, and the error states that getAppletContext() is undefined for the type new ActionListener(). I am wondering if I have to create an object.
    Last edited by NorrinGalan; 05-14-2014 at 02:22 AM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default Re: Exit Java Applet

    From an inner class, you can refer to the enclosing class by:
    TheClassname.this
    Last edited by Norm; 05-14-2014 at 02:31 AM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    NorrinGalan is offline Member
    Join Date
    Feb 2014
    Posts
    12
    Rep Power
    0

    Default Re: Exit Java Applet

    The action listener is:
    Java Code:
             jbtExit.addActionListener(new ActionListener(){
    		@Override
    		public void actionPerformed(ActionEvent e){	
    			this.getAppletContext().showDocument("site.edu/javagroup04");
    		}
    	});

    The error message is:
    Java Code:
    The method getAppletContext() is undefined for the type new ActionListener(){}
    Last edited by NorrinGalan; 05-14-2014 at 02:32 AM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default Re: Exit Java Applet

    Add the outer class's name before the this.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    NorrinGalan is offline Member
    Join Date
    Feb 2014
    Posts
    12
    Rep Power
    0

    Default Re: Exit Java Applet

    I did, and I have this code. The code compiles, but when I run it, it will not close the applet in Eclipse.

    Applet.this.getAppletContext().showDocument(null, "Applet.html");

    Applet is class name and Applet.html is html file associated with applet class file. My class and html files have the same name, except for the extension, of course. These are just generic names. It is the html that I open to run the applet in the browser.
    Last edited by NorrinGalan; 05-14-2014 at 03:09 AM.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default Re: Exit Java Applet

    it will not close the applet in Eclipse.
    Sorry, I don't know anything about eclipse.

    Did you read the API doc for the showDocument() method? Your code doesn't look like it is doing the call correctly.
    What are the args for the method you are trying to call?

    BTW Applet is the name of a Java SE class. It is better NOT to name your classes the same as java classes.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: 06-27-2012, 05:30 PM
  2. Java exit command...
    By sh4rif in forum New To Java
    Replies: 7
    Last Post: 01-18-2012, 12:41 AM
  3. Exit in java
    By deepali in forum Advanced Java
    Replies: 2
    Last Post: 12-28-2011, 09:36 AM
  4. Java Result:-1....on calling exit
    By Cosmos in forum New To Java
    Replies: 2
    Last Post: 07-19-2011, 10:15 PM
  5. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 08:32 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
  •