Question

I have a JMenuBar that has Save, Print, and Exit. I have attempted Exit so far, but cannot get it to work. I would like the Exit to exit the system, Print to print the Total, and the Save to save to a folder. I just need a stear in the right direction. Any help is appreciated.

Here is my code:

[code]
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;

public class cousinsTree extends JApplet implements ActionListener
{
Container Panel;
JMenuBar mnuBar;
JMenuItem mnuExit;
JMenuItem mnuPrint;
JMenuItem mnuSave;
JButton submitButton;
JButton clearButton;
JTextField firstName;
JTextField lastName;
JTextField Address;
JTextField City;
JTextField Total;
JComboBox Service;
JComboBox howOften;
JComboBox numTrees;
LayoutManager setLayout;
String[] TreeList;
String[] numList;
String[] oftenList;

/**
 *
 */
public void init()
{
    Panel = getContentPane();
    this.setLayout(new FlowLayout());
    TreeList= new String[3];
    TreeList [0] = "Trim";
    TreeList [1] = "Chemical Spray";
    TreeList [2] = "Injection";
    numList = new String[3];
    numList [0] = "0-5";
    numList [1] = "6-10";
    numList [2] = "11 >";
    oftenList = new String[3];
    oftenList [0] = "Monthly";
    oftenList [1] = "Quarterly";
    oftenList [2] = "Annually";     
    Panel.setBackground (Color.green);
    submitButton = new JButton("Submit");
    submitButton.addActionListener(this);
    submitButton.setPreferredSize(new Dimension(100,30));
    clearButton = new JButton("Clear");
    clearButton.addActionListener(this);
    clearButton.setPreferredSize(new Dimension(100,30));
    firstName = new JTextField("", 10);
    JLabel lblFirstName = new JLabel("First Name");
    lastName = new JTextField("", 10);
    JLabel lblLastName = new JLabel("Last Name");
    Address = new JTextField("", 15);
    JLabel lblAddress = new JLabel("Address");
    City = new JTextField("Columbus", 10);
    JLabel lblCity = new JLabel("City");
    Total = new JTextField("", 10);
    JLabel lblTotal = new JLabel("Total");

    //Service = new TextField("Service (Trim, Chemical Spray, or Injection).", 20);
    JLabel lblService = new JLabel("Service");
    Service=new JComboBox(TreeList);

    JLabel lblhowOften = new JLabel("How often?");
    howOften = new JComboBox(oftenList);

    JLabel lblnumTrees = new JLabel("Number of Trees");
    numTrees = new JComboBox(numList);

/* Configuration */
    //add items to panel
    Panel.add(lblFirstName);
    Panel.add(firstName);
Panel.add(lblLastName);
    Panel.add(lastName);
    Panel.add(lblAddress);
    Panel.add(Address);
    Panel.add(lblCity);
    Panel.add(City);
    Panel.add(lblnumTrees);
    Panel.add(numTrees);
    Panel.add(lblService);
    Panel.add(Service);
    Panel.add(lblhowOften);
    Panel.add(howOften);
    Panel.add(submitButton);
    Panel.add(clearButton);
    Panel.add(lblTotal);
    Panel.add(Total);



    this.setSize(new Dimension(375, 275));
    this.setLocation(0,0);

Service.setSelectedIndex (0);
howOften.setSelectedIndex (0);
numTrees.setSelectedIndex (0);

        JMenuBar mnuBar = new JMenuBar();
        setJMenuBar(mnuBar);

        JMenuItem mnuSave = new JMenu("Save", true);
            mnuSave.setMnemonic(KeyEvent.VK_S);
            mnuSave.setDisplayedMnemonicIndex(0);
            mnuBar.add(mnuSave);
                            mnuSave.addActionListener(this);

                    JMenuItem mnuPrint = new JMenu("Print", true);
                            mnuPrint.setMnemonic(KeyEvent.VK_P);
                            mnuPrint.setDisplayedMnemonicIndex(0);
                            mnuBar.add(mnuPrint);
                            mnuPrint.addActionListener(this);

        JMenuItem mnuExit = new JMenu("Exit", true);
            mnuExit.setMnemonic(KeyEvent.VK_X);
            mnuExit.setDisplayedMnemonicIndex(0);
            mnuBar.add(mnuExit);
                            mnuExit.addActionListener(this);
}            


@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == clearButton) {

     firstName.setText("");
     lastName.setText("");
     Address.setText("");
     City.setText(""); 
     Total.setText("");
}

if(e.getSource()== submitButton) {
int Selection;
int timesPerYear = 0;
int serviceCost = 0;
double rate = 0, serviceRate = 0;
double result = 0;

Selection = howOften.getSelectedIndex();
if(Selection == 0) {
    timesPerYear = 12;
} else if (Selection == 1) {
    timesPerYear = 4;
} else if (Selection == 2) {
    timesPerYear = 1;
}
Selection = Service.getSelectedIndex();
if(Selection == 0) {
    serviceCost = 20;
} else if (Selection == 1) {
    serviceCost = 25;
} else if (Selection == 2) {
    serviceCost = 30;
}
Selection = numTrees.getSelectedIndex();
if(Selection == 0) {
    rate = 5;
} else if(Selection == 1) {
    rate = 10;
} else if(Selection == 2) {
    rate = 15;
}

DecimalFormat twoDigits = new DecimalFormat("$#,###.00");
result = (serviceCost+rate)*timesPerYear;
Total.setText("" + twoDigits.format(result) + "");
} 
if(e.getSource() == mnuExit) {
   System.out.print("Exiting");
   System.exit(0);
    }
}
}
[/code] 
Was it helpful?

Solution

System.exit(0);

Even a trusted applet cannot end the JVM that other applets might be running in. At least not directly.

Use AppletContext.showDocument(URL) instead.

this.getAppletContext().showDocument(thanksForUsingUrl);

By replacing the page containing the applet with thanksForUsingUrl the stop() & destroy() methods for each applet in the page should be called before (finally) the JVM is ended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top