• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.exit(0) applet

 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class first extends JApplet implements ActionListener{
JButton btn=new JButton("EXIT");

public void init(){
Container con=getContentPane();
con.add(btn);
btn.addActionListener(this);
}

public void actionPerformed(ActionEvent ae){
String st=ae.getActionCommand();
if("EXIT".equals(btn))
System.exit(0);


}
}

Helo frnd above code is runing fine its size and other information i have decleared in first.html. I whish that when i click on EXIT button of applet as soon as the applet window should be closed that's why i have implement System.exit(0) method in if statment of actionPerformed method but it is not working why? is there any problem with exit command or somthing else.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're comparing "EXIT" to "btn", when you should be comparing it to "st".

This will cause appletviewer to quit, but you're aware that it won't cause a browser to quit, right?
 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry sir By the above code applateviewer also not quiting


I would like a statment that should quit at least appletviewer what statment should i add in place of that System.exit(0) pls tell me.


Regard
Arun
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, you're also missing a

btn.setActionCommand("EXIT");

If then you get an "access denied" exception, that means that the appletviewer you're using enforces the applet sandbox restrictions. The two principal ways around that -signed applets and policy files- are described on this wiki page.
[ July 14, 2006: Message edited by: Ulf Dittmer ]
 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Sir still it is not working I have allready added ststment what U told me.

With Regard

Arun kumar maalik
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class today extends JApplet implements ActionListener{

JButton btn=new JButton("EXIT");

Container cont;
public void init(){
cont=getContentPane();
cont.setLayout(null);
btn.setBounds(50,50,90,30);
cont.add(btn);
btn.addActionListener(this);
btn.setActionCommand("EXIT");
}

public void actionPerformed(ActionEvent ae){
JButton button = (JButton)ae.getSource();
String st = button.getActionCommand();

// to identify the button by its reference
if(button == btn)
System.exit(0);
// to identify the button by its actionCommand

/* if("EXIT".equals(st))
System.exit(0);*/
}
}
OOOOPS sir there are a lot of exception are thrown when i m using above code now i have more eager to know How a button event will work when i m working with Button well provide me a bit a tested code so that i can run that code on my computer

wit regard

Arun
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun, you need to put a little effort into it. The least you can do is to post the exceptions you're getting, which will enable us to take a guess at what's going wrong. You could also work through the button section of the Java tutorial to learn concepts such as action listeners.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic