Let Us Know About Applet Programming In Java


This resource contains the information about applet programming and applet class methods in java etc.

Let Us Know About Applet Programming In Java



Java contains two types of programming.
1. Application programming
2. Applet programming


1. Application programming:
It is used to display CUI (Character User Interface) based output.
2. Applet programming:
It is used to display GUI (Graphical User Interface) based output. It contains applet window. Applet class is used to create applet window. This applet class is in java.applet package.

Create public child class from Applet class to display applet window.
Example 1 :
MyApplet.java

import java.applet.Applet
public class MyAplet extends Applet
{
}

example 2:
MyApplet.java
import java.awt.Graphics;
import java.awt.Font;
import java.applet.Applet;
public class MyApplet extends Applet
{
String str="";
public void init()
{
setFont(new Font("",1,14));
str=str+" init ";
}

public void start()
{
str=str+" start ";
}

public void paint(Graphics g)
{
str=str+" paint ";
g.drawString(str,100,200);
}

public void stop()
{
str=str+" stop ";
}
public void destroy()
{
System.out.println("destroy called");
try
{
Thread.sleep(5000);
}catch(InterruptedException e){}
}
}
Applet class contains few overridable , public methods.applet

1. init() : it is called when Applet window is loaded into memory.

2. start() : It is called when Applet window is displayed on the screen.

3. stop() : It is called when Applet window is closed.

4. paint() : It is called immediately after start() method is called. It is also called when Applet window is resized. It contains Graphics class type of argument. This class is in java.awt package. It contains drawString() method. It displays given string at given coordinates place on the Applet window.

5. destroy() : It is called when Applet container window is stopped.

Note 1: init(), destroy() are called only once and remaining methods are called many times. setFont() method takes Font class type object and applies that font properties to Applet window. Font class constructor takes font name, style, size. Font class is in java.awt package (Abstract Window Toolkit).
Note 2: java.awt package contains classes for all controls like Label, Button, Checkbox, List and so on.

LabelDemo.java
Label class creates label control. It is used to display static text. Its constructor takes caption of label. add() method of Applet class adds controls to applet window.

ButtonDemo.java
Button class creates Button control. Its constructor takes caption of button. setLabel() method of Button class is also used to set caption of button. add() method of Applet class adds Button to Applet window. addActionListener() method of Button class registers Button to ActionListener interface. This keyword creates instance of current class(ButtonDemo). That instance contains information of ActionListener.
When registered Button is pressed, ActionListener interface operates actionPerformed() method. This method contains ActionEvent class type of argument. This argument contains initial address of operated button.
getActionCommand() method of ActionEvent class returns caption of Button.
repaint() method of Applet class calls paint() method.
drawString() method of Graphics class displays given string at given x, y coordinates position.
drawOval() method takes center, x-radius-radius and displays circle/oval at that center. If x-radius and y-radius are same, it displays circle. Otherwise it displays ellipse.




CheckboxDemo.java
Checkbox class is used for square type checkbox creation. ItemListener interface is inherited for checkboxes events handling. Checkbox() constructor takes caption, true for default selection/ false for default deselecting. addItemListener() method registers checkboxes to ItemListener interface. When registered checkbox is operated, ItemListener interface operates itemStateChanged() method and stores that checkbox initial address in ItemEvent type of argument itemStateChanged() method. getState() method is used to return true when checkbox is selected / false when checkbox is deselected.

CBGroup.java
Checkbox class is used for radio/option buttons creation. ItemListener interface is inherited for radio buttons. Checkbox() constructor second argument is CheckboxGroup class object. This object helps to create radio buttons. Only one radio button is selected from group. getSelectedCheckbox method of CheckboxGroup class returns initial address of selected radio button. getLabel() method of Checkbox class returns caption of radio button.

ChoiceDemo.java
Choice class is used to create dropdown ListBox. ItemListener interface is inherited for Choice box. Its add() method adds options to choice control. getItem() method of ItemEvent class returns selected option of dropdown list box as Object class. It is type of caste to String type. getSelectedItem() method of Choice class returns selected item name from dropdown list box.

ListboxDemo.java
Listbox class creates list control. It is used for single/multiple selection. Its constructor takes number of visible items, true for multi selection/false for single selection. Its select method takes index and selects that index position value. getSelectedIndexes() method of List class returns selected items indexes as array. getItem() method takes index and returns that index position string.

TextFieldDemo.java
TextField class helps to create textbox. Its setText() method displays text in the textbox Its select() method selects characters from starting index to ending index-1. Its constructor takes size of textbox Its setEchoChar() method sets displaying character for textbox. Its getText() method returns all displaying text in the textbox. Its getSelectedText() method returns selected text in the textbox.

TextAreaDemo.java
It is used to display multilane text. Its constructor takes string, number of visible lines, and number of visible characters in each line.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: