Creating a Simple Java Applet

In this article you will find information about the creating of simple applet using java.awt. Java applet is a small application created using Java language. The user starts applet from the web-browser and it's executed by the Java Virtual machine. The parameters of the applet are received from the web page and they can be used during the applet execution. We will create an Applet, that takes some information from the menu of choice lists and inputs the result of our choice. The first step to create an applet is to derive our class from Applet class. That's why we need to make the next import: import java.applet.*; Now we can declare the applet class: public class Menu extends Applet The Menu applet consists of three choice lists, a button and a text area to output the result of the choice:
  1. private Choice country;
  2. private Choice language;
  3. private Choice age;
  4. private Button send;
  5. private TextField info;
The initialization of applet's components should be done in the init() method. The applet can take some parameters from the web page. The parameters can be uploaded into the program by using String getParameter(String paramName) method. We will get two parameters from the web page - height and width of the applet:
  1. int width = Integer.parseInt(getParameter("width"));
  2. int height = Integer.parseInt(getParameter("height"));
The parseInt(String) method is called because of the fact the getParameter(String) method returns String representation of the parameter. We will use the height and width variables to set the size of the applet:
  1. resize(width,height);
The resize(int,int) method resizes the applet's frame. Now we need to initialize the applet's components. This is the initialization of choice components:
  1. country = new Choice();
  2. language = new Choice();
  3. age = new Choice();
The text area must be initially empty:
  1. info = new TextField();
And now, lets create a button with text:
  1. send = new Button();
  2. send.setLabel("Submit");
I decided to set a special background for my choice lists. I created my own color and set it as a background color:
  1. Color darkPink = new Color(255,20,147);
  2. country.setBackground(darkPink);
  3. language.setBackground(darkPink);
  4. age.setBackground(darkPink);
The next step is to add elements to the choice list. For this, add method can be used:
  1. country.add("Moldova, Republic of");
  2. country.add("Russian Federation");
  3. country.add("Ukraine");
  4. country.add("Romania");
  5. country.add("Belarus");
  6. country.add("USA");
  7. country.add("UK");
Now, I want to set the size of my list. In the applet should be 3 choice list, so the width of one is width/3.This can be done by calling  setBounds(int x,int y, int width,int height):
  1. country.setBounds(0, 0,width/3,50);
The same action are performed on other two choices:
  1. language.setBackground(darkPink);
  2. language.add("Russian");
  3. language.add("Moldavian");
  4. language.add("Ukrainian");
  5. language.add("English");
  6. language.add("Belarusian");
  7. language.setBounds(width/3, 0,width/3,50);
  8.  
  9. age.setBackground(darkPink);
  10. age.add("0-10");
  11. age.add("10-20");
  12. age.add("20-30");
  13. age.add("30-40");
  14. age.add("40-50");
  15. age.add("50 and more");
  16. age.setBounds(2*width/3, 0,width/3,50);
Now,I add this elements to the applet's frame:
  1. add(age);
  2. add(country);
  3. add(language);
The next step is to initialize the button
  1. send</send>. The purpose of this button is to show the selection from the choice lists. That's why we need to add action listener to this button.
  2. The applet class will be the listener, so we need to change the class definition to:
  3. <java>
  4. public class Menu extends Applet implements ActionListener
And now we can set size and add action listener to the send button:
  1. send.setBounds(width/3, 170, width/3, 30);
  2. send.addActionListener(this);
  3. add(send);
The class, that implements ActionListener interface must override
  1. public void actionPerformed(ActionEvent arg0)
method. Eclipse can generate this method automatically. Now, we will finish the implementation of applet's GUI and then will override the necessary method. The last step is to add info text area to the applet:
  1. info.setBounds(0, 120, width, 30);
  2. info.setBackground(Color.red);
  3. add(info);
As an example of overriding another applet method, I change the background of frame in paint. It's really not so useful, because the method is called when an applet needs to be painted. It's just an example:
  1. public void paint(Graphics g){
  2.         setBackground(Color.pink);
  3. }
The GUI is finished. Now we can override the actionPerformed method. Because of the fact, we have only one item, that can produce events, we have no need to check, what is the source of the event. That's why the overridden method looks very simple:
  1. public void actionPerformed(ActionEvent arg0) {
  2.         // TODO Auto-generated method stub
  3.         info.setText("You are "+age.getSelectedItem()+" years old citizen of "+country.getSelectedItem()+" who speaks "+language.getSelectedItem());   
  4. }
Here, we get the selected items from all choice lists and set a String to the textArea according to the collected data. To run an applet with the specified parameters, you need to chose from menu "Run" "Run configurations" options and input all the parameters in the parameters page: config You can specify an list of parameters such as "font","sontSize","background" ... on the web page, that will be used by your applet. You can try this code by yourself. As an exercise, add new item to the choice list and any other buttons to get info separately from all lists.

Add new comment