Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Make java program created run on the web

My basic program:

/*
 * File: OrderSlip.java
 * Description: This class prints an order slip based on stock
 *  ordered from a warehouse
 */
import java.util.*;

public class OrderSlip
{
    public static void main(String[] args) // Main method
    {
            int ITEMCODE, STOCK;
            double PRICE;
            char OUTLET;
            
            Scanner sc = new Scanner(System.in);
                        
            System.out.println("Enter item code: ");
            ITEMCODE = sc.nextInt();
            System.out.println("Enter item price: ");
            PRICE = sc.nextDouble();
            System.out.println("Enter quantity in stock: ");
            STOCK = sc.nextInt();
            System.out.println("Enter outlet placing order (A,B,C...): ");
            OUTLET = sc.next().charAt(0);
            
            System.out.println("      *** ORDER SLIP ***");
            System.out.println("");
            System.out.println("item code           "+ITEMCODE);
            System.out.println("item price          "+PRICE);
            System.out.println("quantity in stock   "+STOCK);
            System.out.println("");
            System.out.println("");
            System.out.println("TOTAL COST          "+PRICE*STOCK);
            System.out.println("");
            System.out.println("");
            System.out.println("send to base        "+OUTLET);
            
    } //end main
} //end class OrderSlip


I'd like to make this program into an applet, then embed it into an html page and run it via IE.
Please provide step by step, I know NetBeans can do this, but it's too complicated. That's why I need your help.
0
ishcmc
Asked:
ishcmc
  • 4
1 Solution
 
Peter KwanCommented:
For a tutorial on applet, please go to the following site:

http://java.sun.com/docs/books/tutorial/deployment/applet/getStarted.html
0
 
ADSLMarkCommented:
Hah! Difficult, you cannot do the stuff in an applet as you do now. Applets are graphical, so you will have to make textfields and textareas.

First we change the class declaration (this requires some additional libraries):

public class OrderSlip
=>
public class OrderSlip extends Applet

Next the program entry is different, namely:

public static void main(String[] args)
=>
public void init()

Next we create a simple user interface:

        //Create components
        this.txtITEMCODE = new TextField();
        this.txtPRICE = new TextField();
        this.txtSTOCK = new TextField();
        this.txtOUTLET = new TextField();
        this.txtResult = new TextArea();
        this.butOk = new Button("Enter");

        //Initialize components
        this.txtResult.setEditable(false);

        //Add components
        Panel pnlInput = new Panel();
        pnlInput.setLayout(new GridLayout(5, 2));
        pnlInput.add(new Label("Enter item code: "));
        pnlInput.add(txtITEMCODE);
        pnlInput.add(new Label("Enter item price: "));
        pnlInput.add(txtPRICE);
        pnlInput.add(new Label("Enter quantity in stock: "));
        pnlInput.add(txtSTOCK);
        pnlInput.add(new Label("Enter outlet placing order (A,B,C...): "));
        pnlInput.add(txtOUTLET);
        pnlInput.add(new Label());
        pnlInput.add(butOk);

        //Set layout
        this.setLayout(new BorderLayout());
        this.add(pnlInput, BorderLayout.NORTH);
        this.add(txtResult, BorderLayout.CENTER);

Next we have to add some listeners to the applet to react to the button press. After that we have to implement the actionPerformed method. So adding the input/ouput code there:

            try
            {
                //Read values
                int ITEMCODE = Integer.valueOf(txtITEMCODE.getText());
                double PRICE = Double.valueOf(txtPRICE.getText());
                int STOCK    = Integer.valueOf(txtSTOCK.getText());
                char OUTLET  = txtOUTLET.getText().charAt(0);

                //Output values
                String res = "      *** ORDER SLIP ***";
                res += "\n\n";
                res += "item code\t"+ITEMCODE+"\n";
                res += "item price\t"+PRICE+"\n";
                res += "quantity in stock\t"+STOCK+"\n";
                res += "\n\n";
                res += "TOTAL COST\t"+(PRICE*STOCK)+"\n";
                res += "\n\n";
                res += "send to base\t"+OUTLET+"\n";
                this.txtResult.setText(res);
            }
            catch(NumberFormatException nfe)
            {
                this.txtResult.setText("Number format exception, please check your input.");
            }
            catch(IndexOutOfBoundsException ioobe)
            {
                this.txtResult.setText("Please enter an outlet placing order (A,B,C...).");
            }

Like that and you got the program almost done. Just some extra declarations and some methods calls and you end up with:

/*
 * File: OrderSlip.java
 * Description: This class prints an order slip based on stock
 *  ordered from a warehouse
 */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class OrderSlip extends Applet implements ActionListener
{
    TextField txtITEMCODE;
    TextField txtPRICE;
    TextField txtSTOCK;
    TextField txtOUTLET;
    TextArea txtResult;

    Button butOk;

    public void init() // Main method
    {
        //Create components
        this.txtITEMCODE = new TextField();
        this.txtPRICE = new TextField();
        this.txtSTOCK = new TextField();
        this.txtOUTLET = new TextField();
        this.txtResult = new TextArea();
        this.butOk = new Button("Enter");

        //Initialize components
        this.txtResult.setEditable(false);

        //Add components
        Panel pnlInput = new Panel();
        pnlInput.setLayout(new GridLayout(5, 2));
        pnlInput.add(new Label("Enter item code: "));
        pnlInput.add(txtITEMCODE);
        pnlInput.add(new Label("Enter item price: "));
        pnlInput.add(txtPRICE);
        pnlInput.add(new Label("Enter quantity in stock: "));
        pnlInput.add(txtSTOCK);
        pnlInput.add(new Label("Enter outlet placing order (A,B,C...): "));
        pnlInput.add(txtOUTLET);
        pnlInput.add(new Label());
        pnlInput.add(butOk);

        //Set layout
        this.setLayout(new BorderLayout());
        this.add(pnlInput, BorderLayout.NORTH);
        this.add(txtResult, BorderLayout.CENTER);

        //Set action listener
        butOk.addActionListener(this);
    } //end main

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==butOk)
        {
            try
            {
                //Read values
                int ITEMCODE = Integer.valueOf(txtITEMCODE.getText());
                double PRICE = Double.valueOf(txtPRICE.getText());
                int STOCK    = Integer.valueOf(txtSTOCK.getText());
                char OUTLET  = txtOUTLET.getText().charAt(0);

                //Output values
                String res = "      *** ORDER SLIP ***";
                res += "\n\n";
                res += "item code\t"+ITEMCODE+"\n";
                res += "item price\t"+PRICE+"\n";
                res += "quantity in stock\t"+STOCK+"\n";
                res += "\n\n";
                res += "TOTAL COST\t"+(PRICE*STOCK)+"\n";
                res += "\n\n";
                res += "send to base\t"+OUTLET+"\n";
                this.txtResult.setText(res);
            }
            catch(NumberFormatException nfe)
            {
                this.txtResult.setText("Number format exception, please check your input.");
            }
            catch(IndexOutOfBoundsException ioobe)
            {
                this.txtResult.setText("Please enter an outlet placing order (A,B,C...).");
            }
        }
    }
} //end class OrderSlip

You can change some components to be different ones, like drop-down lists or other things. You can also add more checks by the input or different settings for the component (like non-editable of the textarea). For example adding maximum length is a possibility and there are a lot of other "nice features". User interface is a difficult field. :-)

Oh i almost forgot, in the HTML file you have to add this line:

<applet code="OrderSlip" width="600" height="600"></applet>

Specifying the width x height of the applet.
Note: html page and OrderSlip.class should be in the same directory (in this case).

Good luck.
Mark
0
 
ADSLMarkCommented:
Oh, you can remove the line:

import java.util.*;

Not necessary any more (not that it matters, but just a small thing i saw when i scrolled up :-)..)

Mark
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ishcmcAuthor Commented:
Wow, this is just a simple program but I already have to change alot of stuff inside.
Is there a program that automatically create a simple user interface, add listeners, implement the actionPerformed method for a .java?
Oh by the way, after changing all those stuff, I save it as .class right?
0
 
ADSLMarkCommented:
I don't know if there are tools to do this for you, but i just created this layout by hand..

You can probably also simulate the console in an applet, but if you have the ability to use user controls, why not use them? It is a lot easier for people to enter data etc.

You can also solve this by asking the user questions with JOptionPane in the Swing package. Then the user simply gets 4 input messageboxes in sequence and the answer is output as a messagebox. I think the layout solution is way nicer.

You can add the whole code in the file "OrderSlip.java" then from the command line its: "javac OrderSlip.java" (without quotes) for compilation, of course this is on a java developer machine, so you probably have to do a little bit different, anyway.. the compiler will create a .class file. In the same directory create a HTML page with some content and the <applet> tag. Open de HTML page in a browser and voila.

Mark
0
 
ADSLMarkCommented:
You can also use a visual java editor, like visual J++, to create a layout. It gives pretty nasty/dirty code so I do not recommend it AT ALL, but if you aren't very good in Java and certainly not in java layout stuff, then that might be a solution.

Most of the time, creating a layout in Java is just a lot of copy-paste work, so if you have the basic ideas then you should be able to make your own layout fairly easy.

Mark
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now