FileDocCategorySizeDatePackage
SumUp.javaAPI DocExample3604Sun Dec 27 15:20:34 GMT 1998None

SumUp

public class SumUp extends Applet
SumUp is a simple applet that adds up some numbers. This version is for JDK 1.0 and later; code for 1.1 is present but commented out.

To make this ready for production, we should implement a little language either in HTML PARAMs, such as

<PARAM NAME=title1 value="Option One">
<PARAM NAME=values1 value="0|100|200|300|400">
<PARAM NAME=title1 value="Option Two">
<PARAM NAME=values1 value="0|400|600|800|1000">

or
in a configuration file which we download and parse (see TreeLink.java in this directory) or load as a Properties file (see MenuIntl.java) - note that as of today we can't load Properties files into an Applet for some reason.

Also, of course, the URL to go to should be a PARAM. Not to mention the colors (see ColorName and/or XColor).

author
Ian F. Darwin, ian@darwinsys.com

Fields Summary
protected Choice[]
cs
The array of Choice items
protected int
numChoices
How many are actually in the array.
protected Label
resultField
The result of the summation
protected Button
sendButton
The pushbutton to send the form in
Constructors Summary
Methods Summary
public booleanaction(java.awt.Event e, java.lang.Object name)
action() is called when a "high level" action happens (like the user pushing a Button!) in one of the components added to this Applet. In JDK1.1 this would be actionPerformed(), but the customer needed this Applet to work on the Internet at a time when many users' browsers were still at JDK1.0.

	// 1.0
		int total = 0;
		for (int i=0; i<numChoices; i++) {
			String text = cs[i].getSelectedItem();
			// System.err.println("Selection " + i + " = " + text);
			int value = Integer.parseInt(text);
			total += value;
		}
		resultField.setText(Integer.toString(total));

		// Now, if that was the Send button, do it.
		if (sendButton == e.target) {
			try {
				URL myNewURL = new URL(
					"http://server/cgi-bin/credit?SUM=" + total);

				// System.out.println("URL = " + myNewURL); // debug...

				// "And then a miracle occurs..."
				getAppletContext().showDocument(myNewURL);

			} catch (Exception err) {
				System.err.println("Error!\n" + err);
				showStatus("Error, look in Java Console for details!");
			}
		}
		return true;		// (1.0) yes, we did something, thank you.
	
public voidinit()
init() is an Applet method called by the browser to initialize


	            
	   
		setBackground(Color.magenta);
		// The layout of the Applet is a Grid; always add things in pairs!
		setLayout(new GridLayout(0,2));
		Choice c;
		add(new Label("Option 1"));
		add(c = new Choice());
			c.addItem("0");
			c.addItem("100");
			c.addItem("200");
			c.addItem("400");
		cs[numChoices++] = c;

		add(new Label("Option 2"));
		add(c = new Choice());
			c.addItem("0");
			c.addItem("100");
			c.addItem("200");
			c.addItem("400");
		cs[numChoices++] = c;

		Panel p = new Panel();
		p.setBackground(Color.pink);
		p.add(new Label("Total:"));
		p.add(resultField = new Label("000000"));
		add(p);

		sendButton = new Button("Send it");
		add(sendButton);						// connect Button into Applet
		// Next line needed for 1.1 version
		// sendButton.addActionListener(this);	// connect it back to Applet
		// or, better yet, have its own actionListener separate from that
		// used by all the Choice items.