Solved

Make java program created run on the web

Posted on 2006-11-19
6
245 Views
Last Modified: 2012-05-05
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
Comment
Question by:ishcmc
  • 4
6 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 17975100
For a tutorial on applet, please go to the following site:

http://java.sun.com/docs/books/tutorial/deployment/applet/getStarted.html
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 500 total points
ID: 17975111
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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17975115
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:ishcmc
ID: 17975163
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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17975308
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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17975325
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question