Solved

Make java program created run on the web

Posted on 2006-11-19
6
258 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
Independent Software Vendors: 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!

 

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 39
Can anyone please tell me why i can not find ID  investorCoolGrid inside the jsp page? 1 23
servlet example 17 59
Coding for the first time 9 65
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

756 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