Solved

Make java program created run on the web

Posted on 2006-11-19
6
224 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ishcmc
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 41
factorial example challenge 10 60
countPairs challenge 7 57
Securing Jmx Console and web console 2 45
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now