Applet won't exit, throws exception...help

The exit button won't close my applet for some reason. I'm getting security permission exceptions.

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

import java.awt.Button;

import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Insets;

import java.awt.Label;

import java.awt.LayoutManager;

import java.awt.TextArea;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;

import java.text.*;

import java.awt.Choice;

publicclass MortgageAppletGUIextends Applet{

Button calculateButton;

Button clearButton;

Button exitButton;

TextField principalField;

TextField interestField;

TextField termField;

TextArea displayArea;

Label principal;

Label term;

Label interest;

Image house;

Font title =new Font("TimesRoman", Font.BOLD, 16);

Font text =new Font("Courier", Font.BOLD, 12);

double monthlyInt;

double monthlyPayment;

int months=12;

double interestPayment;

double principalPayment;

double remainingBalance;

Choice principalChoice;

Choice interestChoice;

Choice termChoice;

publicvoid init(){

MortgageAppletLayout customLayout =new MortgageAppletLayout();

setFont(new Font("Helvetica", Font.PLAIN, 12));

setLayout(customLayout);

calculateButton =new Button("Calculate");

calculateButton.addActionListener(new CalculateActionListener());

add(calculateButton);

displayArea =new TextArea();

add(displayArea);

principal =new Label("Principal");

add(principal);

interest =new Label("Interest");

add(interest);

term =new Label("Term");

add(term);

clearButton =new Button("Clear");

clearButton.addActionListener(new CancelActionListener());

add(clearButton);

principalField =new TextField(20);

add(principalField);

interestField =new TextField(20);

add(interestField);

termField =new TextField(20);

add(termField);

principalChoice =new Choice();

principalChoice.addItem("200000");

principalChoice.addItemListener(new ChoicePrincipal());

add(principalChoice);

interestChoice =new Choice();

interestChoice.addItem("5.35");

interestChoice.addItem("5.5");

interestChoice.addItem("5.75");

interestChoice.addItemListener(new ChoiceInterest());

add(interestChoice);

termChoice =new Choice();

termChoice.addItem("7");

termChoice.addItem("15");

termChoice.addItem("30");

termChoice.addItemListener(new ChoiceTerm());

add(termChoice);

exitButton =new Button("Exit");

exitButton.addActionListener(new ExitActionListener());

add(exitButton);

setSize(getPreferredSize());

house = getImage(getCodeBase(),"house.jpg");

}

class CalculateActionListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

NumberFormat Currency = NumberFormat.getCurrencyInstance();

String userInputprincipal = principalField.getText();

String userInputinterest = interestField.getText();

String userInputterm = termField.getText();

double userInputprincipalD=0;

double userInputinterestD=0;

double userInputtermD=0;

try{

userInputprincipalD = Double.parseDouble(userInputprincipal);

}catch (Exception ex){

JOptionPane.showMessageDialog(null,"Error principalField","Exception 01", JOptionPane.ERROR_MESSAGE);

principalField.requestFocus();

return;

}

try{

userInputinterestD = Double.parseDouble(userInputinterest);

}catch (Exception ex){

JOptionPane.showMessageDialog(null,"Error interestField","Exception 02", JOptionPane.ERROR_MESSAGE);

interestField.requestFocus();

return;

}

try{

userInputtermD = Double.parseDouble(userInputterm);

}catch (Exception ex){

JOptionPane.showMessageDialog(null,"Error termField","Exception 03", JOptionPane.ERROR_MESSAGE);

termField.requestFocus();

return;

}

// Display principal, interest, and loan balance of entire term

double monthlyInt;

double monthlyPayment;

double interestPayment;

double principalPayment;

double remainingBalance;

double months = userInputtermD * 12;

monthlyInt = (userInputinterestD / 100) / 12;

monthlyPayment = (userInputprincipalD * monthlyInt) / (1 - Math.pow(1 / (1 + monthlyInt), userInputtermD * 12));

for (int i = 1; i <= months; ++i){

interestPayment = userInputprincipalD * monthlyInt;

principalPayment = monthlyPayment - interestPayment;

remainingBalance = userInputprincipalD - principalPayment;

displayArea.append("\n#" + i +"\tP=" + Currency.format(principalPayment) +"\tI=" + Currency.format(interestPayment)

+"\tB=" + Currency.format(remainingBalance) +"\n");

userInputprincipalD -= principalPayment;// calculate until payments are done

}

}

}

class CancelActionListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

principalField.setText("");

interestField.setText("");

termField.setText("");

displayArea.setText("");

}

}

class ExitActionListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

System.exit(0);

}

}

class ChoicePrincipalimplements ItemListener{

publicvoid itemStateChanged(ItemEvent ie){

Choice cb1 = (Choice)ie.getSource();// the source of the event is the combo box

String principalC = (String)cb1.getSelectedItem();

principalField.setText(principalC);

}

}

class ChoiceInterestimplements ItemListener{

publicvoid itemStateChanged(ItemEvent ie){

Choice cb1 = (Choice)ie.getSource();// the source of the event is the combo box

String interestC = (String)cb1.getSelectedItem();

interestField.setText(interestC);

}

}

class ChoiceTermimplements ItemListener{

publicvoid itemStateChanged(ItemEvent ie){

Choice cb1 = (Choice)ie.getSource();// the source of the event is the combo box

String termC = (String)cb1.getSelectedItem();

termField.setText(termC);

}

}

publicvoid paint(Graphics g){

// Display program info

g.drawImage(house, 10, 5,this);

g.setColor(Color.green);

g.drawLine(20, 4, 230, 4);

g.setColor(Color.red);

g.setFont(title);

g.drawString("\tMortgage Loan Calculator", 10, 17);

g.setColor(Color.black);

g.setFont(text);

g.drawString("\tCreated by Me", 10, 30);

}

publicstaticvoid main(String args[]){

MortgageAppletGUI applet =new MortgageAppletGUI();

Frame window =new Frame("MortgageApplet");

window.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

System.exit(0);

}

});

applet.init();

window.add("Center", applet);

window.pack();

window.setVisible(true);

}

}

class MortgageAppletLayoutimplements LayoutManager{

public MortgageAppletLayout(){

}

publicvoid addLayoutComponent(String name, Component comp){

}

publicvoid removeLayoutComponent(Component comp){

}

public Dimension preferredLayoutSize(Container parent){

Dimension dim =new Dimension(0, 0);

Insets insets = parent.getInsets();

dim.width = 648 + insets.left + insets.right;

dim.height = 375 + insets.top + insets.bottom;

return dim;

}

public Dimension minimumLayoutSize(Container parent){

Dimension dim =new Dimension(0, 0);

return dim;

}

publicvoid layoutContainer(Container parent){

Insets insets = parent.getInsets();

Component c;

c = parent.getComponent(0);

if (c.isVisible()){c.setBounds(insets.left+8,insets.top+280,72,24);}

c = parent.getComponent(1);

if (c.isVisible()){c.setBounds(insets.left+248,insets.top+88,328,216);}

c = parent.getComponent(2);

if (c.isVisible()){c.setBounds(insets.left+8,insets.top+104,72,24);}

c = parent.getComponent(3);

if (c.isVisible()){c.setBounds(insets.left+8,insets.top+152,72,24);}

c = parent.getComponent(4);

if (c.isVisible()){c.setBounds(insets.left+8,insets.top+128,72,24);}

c = parent.getComponent(5);

if (c.isVisible()){c.setBounds(insets.left+88,insets.top+280,72,24);}

c = parent.getComponent(6);

if (c.isVisible()){c.setBounds(insets.left+80,insets.top+104,72,24);}

c = parent.getComponent(7);

if (c.isVisible()){c.setBounds(insets.left+80,insets.top+128,72,24);}

c = parent.getComponent(8);

if (c.isVisible()){c.setBounds(insets.left+80,insets.top+152,72,24);}

c = parent.getComponent(9);

if (c.isVisible()){c.setBounds(insets.left+152,insets.top+104,72,24);}

c = parent.getComponent(10);

if (c.isVisible()){c.setBounds(insets.left+152,insets.top+128,72,24);}

c = parent.getComponent(11);

if (c.isVisible()){c.setBounds(insets.left+152,insets.top+152,72,24);}

c = parent.getComponent(12);

if (c.isVisible()){c.setBounds(insets.left+172,insets.top+280,50,24);}

}

}

[17986 byte] By [JavaBean5a] at [2007-11-27 4:44:47]
# 1
> I'm getting security permission exceptions.Sounds like you haven't set the policy file.Post the stack trace.
macrules2a at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...
# 2

You can't call System.exit from an applet. Even if the policy file would allow, then you still risk crashing the browser.

Make your exit button call showDocument() to some exit/log off page. (which might be able to have Javascript that can close the browser window). That's about the best you can do.

bsampieria at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...
# 3
> You can't call System.exit from an applet. Even if> the policy file would allow, then you still risk> crashing the browser. Didn't know that.
macrules2a at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...
# 4
what kind of script would that be
JavaBean5a at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...
# 5
Theres got to be a way to shut down the applet without having to do that...is there?
JavaBean5a at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...
# 6

> Theres got to be a way to shut down the applet

No. There is no way to "shut down" an applet. It doesn't even make sense as a concept.

Now if you wrote an applet because you really meant to write a Java application but you were using an old book that started out with applets, I can see how you would think that. But when you realize that an applet is supposed to be a component of a web page, you should see that shutting one down doesn't make sense.

Have you considered writing an application? Or is there a reason for this to be available over the web?

DrClapa at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...
# 7
i see what you mean. I want to close the browser with the exit button.
JavaBean5a at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...
# 8

Maybe some browsers this would work:

applet.showDocument("javascript:window.close();");

But that's probably not reliable. Most likely you just should say:

applet.showDocument("logout.html");

where logout.html has some Javascript

<script language="JavaScript" type="text/javascript">

window.close();

</script>

bsampieria at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...
# 9

> Maybe some browsers this would work:

>

> applet.showDocument("javascript:window.close();");

>

> But that's probably not reliable. Most likely you

> just should say:

>

> applet.showDocument("logout.html");

>

> where logout.html has some Javascript

>

> <script language="JavaScript"

> type="text/javascript">

> window.close();

> </script>

Also display some text on that page like "thanks for using my applet, you can close this window now" for the browsers that don't support javascript.

hunter9000a at 2007-7-12 9:56:54 > top of Java-index,Java Essentials,New To Java...