SlideShare ist ein Scribd-Unternehmen logo
1 von 90
APPLET



•Applet Component
•Layout Manager
•Event Handling
• Adapter Class
Applet Component

    COMPONENT

   Container
   Labels
   Button
   CheckBox
   CheckBoxGroup
   Choice
   Lists
   ScrollBar
   TextField
   TextArea
   MenuBar
   Dialog Boxes
   FileDialog
Applet Component

Label :-

Constructor:-

 new Label( ) throws HeadLessException
 new Label(String str) throws HeadlessException
 new Label(String str,int how) throws HeadlessException

           how=Label.LEFT or Label.RIGHT or Label.CENTER

Methods:-

             void setText(String str)
             String getText();

             void setAlignment(int how)
             int getAlignment();
Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
   Label L1,L2;
   public void init()
    {
       L1=new Label();
       L1.setText(“ABCD”);
       L2=new Label();
       add(L1);
       add(L2);
     }
  }
Applet Component

Button :-
Constructor:-
   new Button() throws HeadlessException
   new Button(String str) throws
                        HeadlessException
Methods:-
    void setLabel(String str); //label for the
                                   buttons.
    String getLabel();
Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
   Button b1,b2;
   public void init()
    {
       b1=new Button();
       b1.setText(“PUSH”);
       L2=new Button();
       add(b1);
       add(b2);
     }
  }
Applet Component
Check Boxes
Constructor :-

 new Checkbox()throws HeadlessException // blank checkbox unchecked
 new Checkbox(String )throws
                              HeadlessException// create a named checkbox
 new Checkbox(String ,boolean ) throws
                        HeadlessException // create a named checkbox if true checkbox is
                                                checked othewise it is false
 new Checkbox(String,boolean,CheckboxGroup) throws
                                 HeadlessException
 new Checkbox(String,CheckboxGroup,boolean) throws
                              HeadlessException
//create a named checkbox with initial stateeither true or false depend on boolean value
     , and
   also declare that check box is a part of checkbox group or not.

Method:-
             boolean getState();
             void setState(String str);
             String getLabel();
Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
   Checkbox c1,c2,c3;
   public void init()
    {
       c1=new Checkbox();
       c2=new Checkbox(“WIN98”);
       c3=new Checkbox(WinXP,null,true);
       c1.setLabel(WIN95“);
       add(c1);
       add(c2);
       add(c3);
     }
  }
Applet Component

CheckboxGroup:- radiobutton only one checkboc at a time .

Constructor:-
           new CheckboxGroup();
Method:-

Checkbox getSelectedCheckbox();
void setSelectedCheckbox(Checkbox which)
Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
   Checkbox c1,c2,c3;
   public void init()
    {
       CheckboxGroup cbg=new CheckboxGroup();
       c1=new Checkbox(WIN(%,cbg,true);
       c2=new Checkbox(“WIN98”,cbg,false);
       c3=new Checkbox(WinXP,cbg,false);
       c1.setLabel(WIN95“);
       add(c1);
       add(c2);
       add(c3);
     }
  }
Applet Component
Choice Controls:- Popup list of item which the
         user may choose.
Constructor :-
            new Choice();
Methods:-
            void add(String name)
           String getSelectedItem();
               int getSelectedIndex();// index start from 0.
            void select(int index);
            void select (String name) ;
            String getItem(int index) ;
Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
  Choice c1;
  public void init()
   {
     Choice c1=new Choice();
     c1.add(“WIN95”);
     c1.add(“WIN98”);
     c1.add(“WINXP”);
     add(c1);

     }
 }
Applet Component
    Lists :- Multiple choice scroll selection list.
    Constructor :-
     new List();
     new List(int numrows);
     new List(int numrows,boolean multiselect)
    Method:-
          void add(String name);
           void add(String name,int index); // index start from 0.
                                                     index=-1 it means add the
                                             item at the end of the list.
            String getSelectedItem(); // index of selected item.
             int getSelectedIndex();         //index start from 0. if more then
                                             one item is selected or no
                                    selection is made -1 is returned.
            String [ ] getSelectedItems() ;//names of selcted items
              int [ ] getSelectedIndexes() ; //indexes of selected items.
Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
    List l1;
    public void init()
        {
                l1=new List(5, true);
                l1.add(“WIN95”);
                l1.add(“WIN98”);
                l1.add(“WINXP”);
                l1.add(“WINVISTA”);
                l1.add(“WIN&”);
                add(l1);
            }
    }
Applet Component

 Scroll Bar :- Used to select continuous values         between
   a specified minimum and                   maximum .
 Constructor:-
 new Scrollbar() // horiozontal scrollbar
 new Scrollbar(int style) //style=Scrollbar.Horizontal
                              Scrollbar.Vertical
 new Scrollbar(int style,int initialvalue,int
                          thumbsize,int min,int max);


  //initialvalue of the scroolbar, thumbsize represent the no of
   represent by the height of the thumb . minimumn and maximum
   value of scrollbar is represented by min and max .
Applet Component
   Scrollbar:-
   Methods :-

void setValues(int initval,int thumbsize,int max,int min);
int getValue();// To retrieve the current value
void setValue(int newvalue);// To set the new value of
                   scrollbar.
int getMinimum();
int getMaximum();
void setUnitIncrement(int newincr);
void setBlockedIncrement(int newincr);

// each time the 1 is incremented from the scroll bar each time it is scrolled
    up or down one line. By default pageup and pagedown value are 10 we
    can change it by above two methods.
Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
   Scrollbar hsb,vsb;
   public void init()
    {
        hsb=new Scrollbar(Scrollbar.Horizontal,0,1,0,50);
        vsb=new Scrollbar(Scrollbar.Vertical,0,1,0,50)
        add(hsb);
         add(vsb);
      }
  }
Applet Component
TextField:-
Costructor:-
 new TextField();
 new TextField(int numchar);// create a textfield numchar
                 wide
 new TextField(String str); // create a text field with str.
 new TextField(String str,int numchar); //create a textfield
                             sets with str and numchar set its width;
Methods:-
 String getText();
 void setText(String str);
 String getSelectedText();
 void select(int startindex,int endindex);//select at startindex and ending at endindex-
  1
 void setEditable();
 boolean isEditable();
Applet Component

import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
  TextField t1;
   public void init()
    {
        t1=new TextField( );
        add(t1);
      }
  }
Applet Component
TextField:-
Costructor:-


 new TextArea();
 new TextArea(int numline,int numchar);
 new TextArea(String str);
 new TextField(String str,int numline,int numchar);
  new TextField(String str,int numline,int numchar,int sbars);// bars= SCROLLBARS_BOTH
                                                              SCROLLBARS_NONE
                                                         SCROLLBARS_HORIZONTAL_ONLY
                                                         SCROLLBARS_VERTICAL_ONLY
Methods:-
  String getText();
 void setText(String str);
 String getSelectedText();
 void select(int startindex,int endindex);//select at startindex and ending at endindex-1
 void setEditable();
 boolean isEditable();
Applet Component

 TextField:-
 Methods:-
 void append(String str); // append the string
                                   specified by str
 void insert(String str,int index);//pass the string
                                  str at specified
                                  by the index
 void replaceRange(String str,int
             startind,int end ind); // replace the
                                      characters from
                                      startindex to
   endIndex-1
Applet Component
    ( MenuBar,Menu,MenuItem)
Constructors:-
MenuBar :-
              new MenuBar();
  Menu:-
                   new Menu();
                   new Menu(String optionname);

 MenuItem:-
                 new MenuItem();
                     new MenuItem(String str);
                     new MenuItem( String str,MenuShortcut keyAccel);
Applet Component
( MenuBar,Menu,MenuItem)
Methods:-
MenuItem:-
      Menuitem add(MenuItem mi);
MenuBar:-
       Menu add(Menu m);
Applet Component
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
    MenuBar mb,Menu m1,m2, MenuItem mi1,mi2,mi3
    public void init()
     {
         mb=new MenuBar();
         m1=new Menu(“File”);
         m2=new Menu(“Edit”);
          mi1=new MenuItem(“New”);
         mi2=new MenuItem(“Open”);
         mi3=new MenuItem(“Cut”);
         m1.add(mi1);
         m1.add(mi2);
         m2.add(mi3)
          mb.add(m1);
          mb.add(m2);
          setMenuBar(mb);
Applet Component
 Container:-

1) Inherit the Component class .
2) It has additional method that allow other component object to
   be nested within it.
3)Container object contain other Container object. This makes
  multilevel containment system.
4)Conatianer is responsible for laying out(positioning) any
   component that it contain.It does this through the
  layout managers.

 methods:-
             void add(Component cmp);
             void remove(Component cmp);
Layout Manager
 Every Component that we place in the Container
  has a size and location in Container.This size and
  location of the component in the Container is
  managed by the LayoutManager. Following are the
  types of LayoutManagers
 FlowLayoutManager
 GridLayoutManager
 BorderLayoutManager
 CardLayoutManager
Layout Manager

 Methods:-
 void setLayout(LayoutManager layoutobj);
  // layoutobj is the object of LayoutManager
 class.if we pass null then we disable the
 layout manager and determine the shape and
 position manually using setBound() defined
 by Component class.
Layout Manager
      FlowLayoutManager :-

        new FlowLayout(); // maintain the space of 5 pixels between each
                          component.

        new FlowLayout(int how)                 // how specify
                                  alignment of component
                           how=FlowLayout.LEFT
                                                                  FlowLayout.RIGHT
                                                        FlowLayout.CENTER
        new FlowLayout(int how,int horz, int vert )

    // horz & vert specify horizontal and vertical space left between each component .
Layout Manager
import java.awt.*;

import java.applet.*;
public class MyApp extends Applet
{
    TextField t1,t2;
    Button b1,b2;
        FlowLayout fl;
    public void init()
        {
                fl=new FlowLayout();
                setLayout(fl);
                   t1=new TextField();
                   t1=new TextField();
                  b1=new Button(“add”);
                  b2=new Button(“sustract”);
                  add(t1);
                   add(t2);
                   add(b1);
                   add(b2);
            }
    }
Layout Manager

 GridLayoutManager:-
 new GridLayoutManager( ); //create a grid of
                                 single row
 and                                    single
 column .
 new GridLayoutManager(int row, int col);
 new GridLayoutManager(int row, int col,int
                              horz,int col);
Layout Manager
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
    TextField t1,t2;
    Button b1,b2;

        GridLayout gl;
    public void init()
        {
                gl=new GridLayout(2,2);
                setLayout(gl);;
                    t1=new TextField();
                    t1=new TextField();
                 b1=new Button(“add”);
                 b2=new Button(“sustract”);
                 add(t1);
                    add(t2);
                    add(b1);
                    add(b2);
            }
    }
Layout Manager
    BorderLayoutManager :-
                      It has four narrow fixed width components at the edges and one large area in the
     center ,The four sides is referred to as north, south,east,west and middle are is referred as center.
     BorderLayout reffers the following constant that specify the regions.

    BorderLayout.CENTER
    BorderLayout.EAST
    BorderLayout.WEST
    BorderLayout.SOUTH
    BorderLayout.NORTH

    All the regions specify with the Component when we add that component in that
     Container.

        void add(Component compobj , Object region);
Layout Manager

 Constructor:-


     new BorderLayoutManager( );

      new BorderLayout(int horz,int vert);
Layout Manager
import java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
  TextField t1,t2;
  Button b1,b2,b3;
   BorderLayout bl;
  public void init()
   {
       bl=new BorderLayout( ;
       setLayout(bl);;
        t1=new TextField();
        t1=new TextField();
         b1=new Button(“add”);
Layout Manager

b2=new Button(“sustract”);
       b3=new Button(“equal”) ;
       add(t1,BorderLayout.EAST);
     add(t2, BorderLayout.WEST;
     add(b1, BorderLayout.SOUTH);
     add(b2, BorderLayout.NORTH);
       add(b2, BorderLayout.CENTER);
   }
 }
Event Handling

 Event:-
       It can be generated as a consequence of
  person interacting with the elements in a gui.
  Some of the activities that cause events to be
  generated are pressing a button ,entring a
  character via a keyboard , selecting a list etc.

 “ In the Event Delegation model,an Event is an
  object that describes a state change in a source.
  ”
Event Handling
 To process the events we need to identify this
  events and then take some action. This is known as event
  handling. The modern approach to handling an event is
  based on “Event Delegation Model”. In this model we
  have 3 following entities .

 Source
 Event
 Handler or Listenr
Event Handling

 Event Delegation Model :-
  (Delegation Event Model)

      Source   generates     Events
                                 Handeled by


               register

                              Handler
Event Handling

 Source :- Source is used to generate the event and it
  is mainly a Component.Examples Button,TextField
  etc. Every source and component has the
  predefined .
 Event :- An Event can be generated by the source.It
  can possible that the same events are generated by
  different sources. Events are represented by classes .
 Handler or Listener:- To handle the event handlers
  are used . Every event is a predefined handler. The
  event can be generated by any source but the
  handler is always be same. Handlers are represented
  by or interfaces.
Event Handling

 ActionEvent is an event is generated when we
  click on button ar press the enter key in TextField
  . And this ActionEvent is handled by
  ActionListener interface.




Button   onclick    ActionEvent   handled by   ActionListener
         on enter

TextField
Event Handling

 ActionListener is an interface which contains
  the following method.
 public void actionPerformed(ActionEvent ae)

 When the ActionEvent is generated the
  ActionListener’s actionPerformed method get
  executed . The operation that we want to
  perform on the click of Button should be put
  within the actionPerformed method.
Event Handling
    For the event handling we need to create the handler class. The class will be
     handler class when it implement the appropriate listener and override the
     methods of interface.
    Ex:-
    public class A implements ActionListener
     {
         public void actionPerformed(ActionEvent ae)
          {
            --------------
            --------------
            --------------
            --------------
           }
       }
Event Handling
Handler 1:-


public class A implements ActionListener
   {
           public void actionPerformed(ActionEvent ae)
            {
                --------------
                --------------
                --------------
                --------------
            }
       }
Handler 2:-


public class B implements ActionListener
   {
           public void actionPerformed(ActionEvent ae)
            {
                --------------
                --------------
                --------------
                --------------
            }
       }
Event Handling(ActionEvent)

   import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    public class MyApp extends Applet
     {
       public TextField t1,t2,t3;
       Button b1;
       FlowLayout fl;
       public void init()
Event Handling(ActionEvent)

 {
     fl=new FlowLayout( );
      setLayout(fl);
     t1=new TextField(5);
     t2=new TextField(5);
        t3=new TextField(5);
      b1=new Button("add");
              add(t1);
     add(t2);
     add(t3);
     add(b1);
Event Handling(ActionEvent)

 AddHandler ah=new AddHandler(t1,t2,t3);
           b1.addActionListener(ah);

       }
   }
Event Handling(ActionEvent)
   import java.awt.*;
   import java.applet.*;
   import java.awt.event.*;
   public class AddHandler implements ActionListener
      {
         TextField th1,th2,th3;
         public AddHandler(TextField t33,TextField t43,TextField t53)
          {
             th1=t33;
             th2=t43;
             th3=t53;
           }



Event Handling(ActionEvent)

 public void actionPerformed(ActionEvent ae)
        {

           int i=Integer.parseInt(th1.getText());
           int j=Integer.parseInt(th2.getText());
           String s1=i+j+ "";
           th3.setText(s1);
         }
     }
Event Handling
 Once the handler is created it should be registered with source without
  which eventhandling is not possible it is necessary otherwise we don’t
  know which handler handle the event.
 To register the handler with the source th methods are provided in the
  source.

    Registration methods of Button and Textfield source is following .

        void addActionListener(ActionListner al)

    // al represent the object of that class which implement the
       ActionListener interface. Method belonging to Button and Textfield
       class.
Event Handling

 Action Event- 1)button is clicked.
                2) list item is double clicked.
                    3)menu item is selected.
                    4) txt field is entered.
ActionListener :-

   public void actionPerformed(ActionEvent ae)
Event Handling

 ItemEvent:- 1) checkbox or list item is
                  selected .
               2) choice selection is made
                3)Checkable menu item is
                selected or deselected.
 ItemListener:-

   public void itemStateChanged(ItemEvent ie)
Event Handling

 AdjustmentEvent :-Scroll bar is manipulated .


 AdjustementListener:-

void adjustmentValueChanged(
                      AdjustmentEvent e);
Event Handling

 ComponentEvent:- when a component is
                     hidden moved resized or
                     become visible.

 ComponentListener:-

void ComponentResized(ComponentEvevnt ce);
void ComponentMoved(ComponentEvevnt ce);
void ComponentShown(ComponentEvevnt ce);
void ComponentHidden(ComponentEvevnt ce);
Event Handling

 FocusEvent :- Component gains or loses
                keyboard focus.

 FocusListener:-

void focusGained(FocusEvent fe);
void focusLost(FocusLost fl);
Event Handling

 ContainerEvent :- Component is added or
                       removed from Container .
ContainerListener :-

void componentAdded(ContainerEvent ce) ;
void componentRemoved(ContainerEvent ce )
Event Handling

 KeyEvent:- Input is received from keyboard .


 KeyListener:-


      void keyPressed(KeyEvent ke);
      void keyReleased(KeyEvent ke);
      void keyTyped(KeyEvent ke);
Event Handling

 MouseEvent :- When mouse is draggd,
  moved,clicked,pressed or
  released .

MouseListener :-

void mouseClicked(MouseEvent me);
void mouseEntered(MouseEvent me);
void mouseDragged(MouseEvent me);
Event Handling

void mousePressed(MouseEvent me);
void mouseExited(MouseEvent me)
Event Handling

MouseWheelEvent:- When mouse wheel is
                   moved .

MouseWheelListener:-

void mouseWheelMoved(MouseWheelEvent
                       mwe );
Event Handling

 TextEvent:- When the value of text area or
              text field is generated .

 TextListener:-

     void textChanged(TextEvent te);
Event Handling

 WindowEvent:- When window is activated,
                    closed , deactivated,
                    deiconified , iconified ,
             opened or quit .
 WindowListener:-
  void windowActivated(WindowEvent we);
  void windowClosed(WindowEvent we);
  void windowClosing(WindowEvent we);
  void windowIconified(WindowEvent we);
 void windowDeiconified(WindowEvent we);
Event Handling

void windowDeactivated(WindowEvent we);
void windowOpened(WindowEvent we);
Inner class

 Classes which is defined within another class.
   class A
       {
              Class B
                  {
                        }
         }
 A is container class or top level class. B is inner class.
 One class can contain any no of inner class./Inner
  classs can contain inner class and this can go upto
  any level.
Inner class

 Inner classes are of 4 types.


1)Static inner class
2)Non static inner class
3)Local inner class
4) Anonymous inner class
Inner class
 Non static inner class:- This class ork as the
                     member of class like
static inner class they can have any type of
 accessibility (public/private/protected/default)
,but like static inner class they can not have static members .
   Because the non static class is the member of top level class and
   non static member exist when its object is created .The non static
   inner class doesn’t exist until the object of top level class created.

    A non static inner class is always associated within the object of
   top level class and therefore it has the special privilege it can
   access any member(static/non static) of its top level class . So
   that non static inner class is used for event handling.
Panel

 A Component which is used as a Container. It
 is used for physically grouping of Component.
Constructor:-
   new Panel(); // Create the panel with Flow
                      layout Manager.
 new Panel(LayoutManager lm); // Create
                        the panel with
                        same
                 layoutManager by
                 which is pass as the parameter
                 in the panel.
Panel

 Methods:-


    add(Component cmp);
    add(Component cmp,int loc);
Standalone application
  To cretae a stand alone application we ned to create a class which will
   inherit Frame class . To run it we don’t need html ,this type of
   application has it ‘s own window. And this type of application created
   just like console based application.
 Features of stand alone application.
1)   Standalone application is                                also called
     Frame.
2)Frame provide the outlaw of window we only           need to place the
   components on window.
3)Frame has the same applarcchitecture and structure as the applet and
   therefore GUI application and the event handling will be same as applet.
4)Like applet it doesn’t have init,start,stop,destroy,paint methods and
   therefore instead of initialize the applet we need we need to define the
   constructor of that class which inherit the Frame class.
5)To display standalone applicatio we need main method.just like console
   based application
Standalone application
       Import java.awt.*;
       Impoort java.applet.*;
       Import java.awt.event.*;

public class Hobby extends Frame implements ItemListener
{
   ---------------
   --------------
 public Hobbbby( )

{
        -----------------
        ------------------
        ------------------ same as init
    }
Standalone application

Public void itemStateChanged(ItemListener il)
  {
                  ----------
     -----
     -------
        }
}
Standalone application

 public class HobbyGui
   {
    psvm(String arg[ ])
{Hobby h=new Hobby();
h.setSize(300,300);
h.setVisible(true);
  }
}
Standalone application
   import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    class MyFrame extends Frame implements ActionListener
     {
       TextField t1,t2,t3;
       Button b1;
       FlowLayout fl;
       public MyFrame()
       {
            fl=new FlowLayout( );
            setLayout(fl);
              t1=new TextField(5);

Standalone application

 t2=new TextField(5);
         t3=new TextField(5);
       b1=new Button("add");
       add(t1);
      add(t2);
      add(t3);
      add(b1);
       b1.addActionListener(this);
    }
Standalone application

 public void actionPerformed(ActionEvent ae)
      {

         int i=Integer.parseInt(t1.getText());
         int j=Integer.parseInt(t2.getText());

         t3.setText(i+j+"");
       }
    }
Standalone application

 public class MyWindow
    {
      public static void main(String arg[])
      {
         MyFrame mf=new MyFrame();
         mf.setSize(300,300);
         mf.setVisible(true);
       }
    }
Standalone application
 Some issues on Window:- When w click on crossbar th window
  doesn’t terminate and still on RAM to close it we have to close the
   DOS prompt because the event is generated by Frame we have
  to handle the event of Frame. There are 7 state of Frame when it
  generate event. In all 7 state it generate the WindowEvent .
                   To handle the 7 state of Frame WindowListener
  interface provide 7 different methods.When we implement the
  WindowListener we need to override the all 7 methods.

   void windowActivated(WindowEvent we);
   void windowClosed(WindowEvent we);
   void windowClosing(WindowEvent we);
Standalone application

void windowIconified(WindowEvent we);
void windowDeiconified(WindowEvent we);
void windowDeactivated(WindowEvent we);
void windowOpened(WindowEvent we);
Standalone application
1) windowOpened is called after the application
display on the screen . It called only once .It is used to initialize the
   application.
2)windowIconified is called when window is minimized .
3) windowDeiconified is called when window is maximized .
4) winowActivate is called when the application get the focus.
5)windowDeactvate is called when the application lost the focus.
6)windowClosed is called when we click on cross button .
7)windowClosing
    dispose():- is called to terminate the application
    System.exit(0) :-is called to terminate the dos prompt or to
                     terminate the JVM.
Standalone application

 windowClosing is called when the application
  is dispose.It is used to save the state of
  application.This is called only once but closed
  is not called untill dispose is called .




 Registration method :-
void addindowListner(WindowEvent we)
 import java.awt.*;
  import java.applet.*;
  import java.awt.event.*;
  class MyFrame extends Frame implements
  ActionListener,WindowListener
 {
     TextField t1,t2,t3;
    Button b1;

 FlowLayout fl;
   public MyFrame()
   {
        fl=new FlowLayout( );
        setLayout(fl);
       t1=new TextField(5);
       t2=new TextField(5);
          t3=new TextField(5);
 b1=new Button("add");
         add(t1);
        add(t2);
        add(t3);
        add(b1);
         b1.addActionListener(this);
    }
 public void actionPerformed(ActionEvent ae)
     {

        int i=Integer.parseInt(t1.getText());
        int j=Integer.parseInt(t2.getText());

        t3.setText(i+j+"");
      }
   public void windowActivated(WindowEvent we) { }
   public void windowClosed(WindowEvent we) {dispose();
  System.exit(0); }
   public void windowClosing(WindowEvent we) { }
Standalone application

 public void windowIconified(WindowEvent we) {
  }
   public void windowDeiconified(WindowEvent
  we){ }
   public void
  windowDeactivated(WindowEvent we) { }
   public void windowOpened(WindowEvent
  we) { }


    }
Standalone application

 public class MyWindowHandler
   {
       public static void main(String arg[])
       {
          MyFrame mf=new MyFrame();
          mf.setSize(300,300);
          mf.setVisible(true);
        }
     }
Adapter class


      WindowListener interface

              implements

       UserDefined class
Adapter class

       WindowListener interface

           implements

        WindowAdapter class

                extends
        UserDefined class
Adapter class
   import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    class MyFrame extends Frame implements ActionListener,WindowAdapter
     {
         ------------------
         ------------------
         ---------------------
      public void windowClosed(WindowEvent we)
        {
             dispose();
             System.exit(0);
         }
      ----------------------
           -----------------

Adapter class

 ---------------------
 }
 public class MyWindowAdapter
     {
         public static void main(String arg[])
         {
          -------------------
           -------------------
           ---------------------
           }
       }
Adapter class

 All listener those contain more than one
  methods have Adapter classs.

 WindowListener .
 KeyListener
 MouseListener
 MouseMotionListener.

Weitere ähnliche Inhalte

Was ist angesagt?

Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
java-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletjava-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletraksharao
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix TaskHermann Hueck
 
Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and JavaSasha Goldshtein
 
Swift internals
Swift internalsSwift internals
Swift internalsJung Kim
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummiesknutmork
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDrRajeshreeKhande
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMMario Fusco
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testingScott Wlaschin
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based TestingC4Media
 

Was ist angesagt? (19)

Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
java-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletjava-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of applet
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix Task
 
Java programs
Java programsJava programs
Java programs
 
28 awt
28 awt28 awt
28 awt
 
Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and Java
 
C# Generics
C# GenericsC# Generics
C# Generics
 
Swift internals
Swift internalsSwift internals
Swift internals
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
 
Java Unit 1 Project
Java Unit 1 ProjectJava Unit 1 Project
Java Unit 1 Project
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testing
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based Testing
 
Java Generics
Java GenericsJava Generics
Java Generics
 

Andere mochten auch (10)

Creating a frame within an applet
Creating a frame within an appletCreating a frame within an applet
Creating a frame within an applet
 
Java Applet
Java AppletJava Applet
Java Applet
 
Graphics programming in Java
Graphics programming in JavaGraphics programming in Java
Graphics programming in Java
 
java applets
java appletsjava applets
java applets
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Applet java
Applet javaApplet java
Applet java
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
Java applets
Java appletsJava applets
Java applets
 
Java applets
Java appletsJava applets
Java applets
 
Applet progming
Applet progmingApplet progming
Applet progming
 

Ähnlich wie Applet

Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdfComplete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdfamericanopticalscbe
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxbradburgess22840
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docxajoy21
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manualChandrapriya Jayabal
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxssuser10ef65
 
Using the code below Part 1 Write a static method called L.pdf
Using the code below Part 1 Write a static method called L.pdfUsing the code below Part 1 Write a static method called L.pdf
Using the code below Part 1 Write a static method called L.pdfpicscamshoppe
 
Stack Implementation
Stack ImplementationStack Implementation
Stack ImplementationZidny Nafan
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfflashfashioncasualwe
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical FileFahad Shaikh
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfinfo114
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 

Ähnlich wie Applet (20)

Awt components
Awt componentsAwt components
Awt components
 
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdfComplete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
AWT New-3.pptx
AWT New-3.pptxAWT New-3.pptx
AWT New-3.pptx
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptx
 
Using the code below Part 1 Write a static method called L.pdf
Using the code below Part 1 Write a static method called L.pdfUsing the code below Part 1 Write a static method called L.pdf
Using the code below Part 1 Write a static method called L.pdf
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
Awt
AwtAwt
Awt
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Collection and framework
Collection and frameworkCollection and framework
Collection and framework
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
Java Generics
Java GenericsJava Generics
Java Generics
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 

Applet

  • 2. Applet Component COMPONENT  Container  Labels  Button  CheckBox  CheckBoxGroup  Choice  Lists  ScrollBar  TextField  TextArea  MenuBar  Dialog Boxes  FileDialog
  • 3. Applet Component Label :- Constructor:- new Label( ) throws HeadLessException new Label(String str) throws HeadlessException new Label(String str,int how) throws HeadlessException how=Label.LEFT or Label.RIGHT or Label.CENTER Methods:- void setText(String str) String getText(); void setAlignment(int how) int getAlignment();
  • 4. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { Label L1,L2; public void init() { L1=new Label(); L1.setText(“ABCD”); L2=new Label(); add(L1); add(L2); } }
  • 5. Applet Component Button :- Constructor:- new Button() throws HeadlessException new Button(String str) throws HeadlessException Methods:- void setLabel(String str); //label for the buttons. String getLabel();
  • 6. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { Button b1,b2; public void init() { b1=new Button(); b1.setText(“PUSH”); L2=new Button(); add(b1); add(b2); } }
  • 7. Applet Component Check Boxes Constructor :- new Checkbox()throws HeadlessException // blank checkbox unchecked new Checkbox(String )throws HeadlessException// create a named checkbox new Checkbox(String ,boolean ) throws HeadlessException // create a named checkbox if true checkbox is checked othewise it is false new Checkbox(String,boolean,CheckboxGroup) throws HeadlessException new Checkbox(String,CheckboxGroup,boolean) throws HeadlessException //create a named checkbox with initial stateeither true or false depend on boolean value , and also declare that check box is a part of checkbox group or not. Method:- boolean getState(); void setState(String str); String getLabel();
  • 8. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { Checkbox c1,c2,c3; public void init() { c1=new Checkbox(); c2=new Checkbox(“WIN98”); c3=new Checkbox(WinXP,null,true); c1.setLabel(WIN95“); add(c1); add(c2); add(c3); } }
  • 9. Applet Component CheckboxGroup:- radiobutton only one checkboc at a time . Constructor:- new CheckboxGroup(); Method:- Checkbox getSelectedCheckbox(); void setSelectedCheckbox(Checkbox which)
  • 10. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { Checkbox c1,c2,c3; public void init() { CheckboxGroup cbg=new CheckboxGroup(); c1=new Checkbox(WIN(%,cbg,true); c2=new Checkbox(“WIN98”,cbg,false); c3=new Checkbox(WinXP,cbg,false); c1.setLabel(WIN95“); add(c1); add(c2); add(c3); } }
  • 11. Applet Component Choice Controls:- Popup list of item which the user may choose. Constructor :- new Choice(); Methods:- void add(String name) String getSelectedItem(); int getSelectedIndex();// index start from 0. void select(int index); void select (String name) ; String getItem(int index) ;
  • 12. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { Choice c1; public void init() { Choice c1=new Choice(); c1.add(“WIN95”); c1.add(“WIN98”); c1.add(“WINXP”); add(c1); } }
  • 13. Applet Component  Lists :- Multiple choice scroll selection list.  Constructor :- new List(); new List(int numrows); new List(int numrows,boolean multiselect) Method:- void add(String name); void add(String name,int index); // index start from 0. index=-1 it means add the item at the end of the list. String getSelectedItem(); // index of selected item. int getSelectedIndex(); //index start from 0. if more then one item is selected or no selection is made -1 is returned. String [ ] getSelectedItems() ;//names of selcted items int [ ] getSelectedIndexes() ; //indexes of selected items.
  • 14. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { List l1; public void init() { l1=new List(5, true); l1.add(“WIN95”); l1.add(“WIN98”); l1.add(“WINXP”); l1.add(“WINVISTA”); l1.add(“WIN&”); add(l1); } }
  • 15. Applet Component  Scroll Bar :- Used to select continuous values between a specified minimum and maximum .  Constructor:- new Scrollbar() // horiozontal scrollbar new Scrollbar(int style) //style=Scrollbar.Horizontal Scrollbar.Vertical new Scrollbar(int style,int initialvalue,int thumbsize,int min,int max); //initialvalue of the scroolbar, thumbsize represent the no of represent by the height of the thumb . minimumn and maximum value of scrollbar is represented by min and max .
  • 16. Applet Component  Scrollbar:-  Methods :- void setValues(int initval,int thumbsize,int max,int min); int getValue();// To retrieve the current value void setValue(int newvalue);// To set the new value of scrollbar. int getMinimum(); int getMaximum(); void setUnitIncrement(int newincr); void setBlockedIncrement(int newincr); // each time the 1 is incremented from the scroll bar each time it is scrolled up or down one line. By default pageup and pagedown value are 10 we can change it by above two methods.
  • 17. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { Scrollbar hsb,vsb; public void init() { hsb=new Scrollbar(Scrollbar.Horizontal,0,1,0,50); vsb=new Scrollbar(Scrollbar.Vertical,0,1,0,50) add(hsb); add(vsb); } }
  • 18. Applet Component TextField:- Costructor:- new TextField(); new TextField(int numchar);// create a textfield numchar wide new TextField(String str); // create a text field with str. new TextField(String str,int numchar); //create a textfield sets with str and numchar set its width; Methods:- String getText(); void setText(String str); String getSelectedText(); void select(int startindex,int endindex);//select at startindex and ending at endindex- 1 void setEditable(); boolean isEditable();
  • 19. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { TextField t1; public void init() { t1=new TextField( ); add(t1); } }
  • 20. Applet Component TextField:- Costructor:- new TextArea(); new TextArea(int numline,int numchar); new TextArea(String str); new TextField(String str,int numline,int numchar); new TextField(String str,int numline,int numchar,int sbars);// bars= SCROLLBARS_BOTH SCROLLBARS_NONE SCROLLBARS_HORIZONTAL_ONLY SCROLLBARS_VERTICAL_ONLY Methods:- String getText(); void setText(String str); String getSelectedText(); void select(int startindex,int endindex);//select at startindex and ending at endindex-1 void setEditable(); boolean isEditable();
  • 21. Applet Component TextField:- Methods:- void append(String str); // append the string specified by str void insert(String str,int index);//pass the string str at specified by the index void replaceRange(String str,int startind,int end ind); // replace the characters from startindex to endIndex-1
  • 22. Applet Component ( MenuBar,Menu,MenuItem) Constructors:- MenuBar :- new MenuBar(); Menu:- new Menu(); new Menu(String optionname); MenuItem:- new MenuItem(); new MenuItem(String str); new MenuItem( String str,MenuShortcut keyAccel);
  • 23. Applet Component ( MenuBar,Menu,MenuItem) Methods:- MenuItem:- Menuitem add(MenuItem mi); MenuBar:- Menu add(Menu m);
  • 24. Applet Component import java.awt.*; import java.applet.*; public class MyApp extends Applet { MenuBar mb,Menu m1,m2, MenuItem mi1,mi2,mi3 public void init() { mb=new MenuBar(); m1=new Menu(“File”); m2=new Menu(“Edit”); mi1=new MenuItem(“New”); mi2=new MenuItem(“Open”); mi3=new MenuItem(“Cut”); m1.add(mi1); m1.add(mi2); m2.add(mi3) mb.add(m1); mb.add(m2); setMenuBar(mb);
  • 25. Applet Component Container:- 1) Inherit the Component class . 2) It has additional method that allow other component object to be nested within it. 3)Container object contain other Container object. This makes multilevel containment system. 4)Conatianer is responsible for laying out(positioning) any component that it contain.It does this through the layout managers. methods:- void add(Component cmp); void remove(Component cmp);
  • 26. Layout Manager  Every Component that we place in the Container has a size and location in Container.This size and location of the component in the Container is managed by the LayoutManager. Following are the types of LayoutManagers  FlowLayoutManager  GridLayoutManager  BorderLayoutManager  CardLayoutManager
  • 27. Layout Manager  Methods:- void setLayout(LayoutManager layoutobj); // layoutobj is the object of LayoutManager class.if we pass null then we disable the layout manager and determine the shape and position manually using setBound() defined by Component class.
  • 28. Layout Manager  FlowLayoutManager :- new FlowLayout(); // maintain the space of 5 pixels between each component. new FlowLayout(int how) // how specify alignment of component how=FlowLayout.LEFT FlowLayout.RIGHT FlowLayout.CENTER new FlowLayout(int how,int horz, int vert ) // horz & vert specify horizontal and vertical space left between each component .
  • 29. Layout Manager import java.awt.*; import java.applet.*; public class MyApp extends Applet { TextField t1,t2; Button b1,b2; FlowLayout fl; public void init() { fl=new FlowLayout(); setLayout(fl); t1=new TextField(); t1=new TextField(); b1=new Button(“add”); b2=new Button(“sustract”); add(t1); add(t2); add(b1); add(b2); } }
  • 30. Layout Manager  GridLayoutManager:- new GridLayoutManager( ); //create a grid of single row and single column . new GridLayoutManager(int row, int col); new GridLayoutManager(int row, int col,int horz,int col);
  • 31. Layout Manager import java.awt.*; import java.applet.*; public class MyApp extends Applet { TextField t1,t2; Button b1,b2; GridLayout gl; public void init() { gl=new GridLayout(2,2); setLayout(gl);; t1=new TextField(); t1=new TextField(); b1=new Button(“add”); b2=new Button(“sustract”); add(t1); add(t2); add(b1); add(b2); } }
  • 32. Layout Manager  BorderLayoutManager :- It has four narrow fixed width components at the edges and one large area in the center ,The four sides is referred to as north, south,east,west and middle are is referred as center. BorderLayout reffers the following constant that specify the regions. BorderLayout.CENTER BorderLayout.EAST BorderLayout.WEST BorderLayout.SOUTH BorderLayout.NORTH  All the regions specify with the Component when we add that component in that Container. void add(Component compobj , Object region);
  • 33. Layout Manager  Constructor:- new BorderLayoutManager( ); new BorderLayout(int horz,int vert);
  • 34. Layout Manager import java.awt.*; import java.applet.*; public class MyApp extends Applet { TextField t1,t2; Button b1,b2,b3; BorderLayout bl; public void init() { bl=new BorderLayout( ; setLayout(bl);; t1=new TextField(); t1=new TextField(); b1=new Button(“add”);
  • 35. Layout Manager b2=new Button(“sustract”); b3=new Button(“equal”) ; add(t1,BorderLayout.EAST); add(t2, BorderLayout.WEST; add(b1, BorderLayout.SOUTH); add(b2, BorderLayout.NORTH); add(b2, BorderLayout.CENTER); } }
  • 36. Event Handling  Event:- It can be generated as a consequence of person interacting with the elements in a gui. Some of the activities that cause events to be generated are pressing a button ,entring a character via a keyboard , selecting a list etc. “ In the Event Delegation model,an Event is an object that describes a state change in a source. ”
  • 37. Event Handling  To process the events we need to identify this events and then take some action. This is known as event handling. The modern approach to handling an event is based on “Event Delegation Model”. In this model we have 3 following entities .  Source  Event  Handler or Listenr
  • 38. Event Handling  Event Delegation Model :- (Delegation Event Model) Source generates Events Handeled by register Handler
  • 39. Event Handling  Source :- Source is used to generate the event and it is mainly a Component.Examples Button,TextField etc. Every source and component has the predefined .  Event :- An Event can be generated by the source.It can possible that the same events are generated by different sources. Events are represented by classes .  Handler or Listener:- To handle the event handlers are used . Every event is a predefined handler. The event can be generated by any source but the handler is always be same. Handlers are represented by or interfaces.
  • 40. Event Handling  ActionEvent is an event is generated when we click on button ar press the enter key in TextField . And this ActionEvent is handled by ActionListener interface.  Button onclick ActionEvent handled by ActionListener on enter TextField
  • 41. Event Handling  ActionListener is an interface which contains the following method. public void actionPerformed(ActionEvent ae)  When the ActionEvent is generated the ActionListener’s actionPerformed method get executed . The operation that we want to perform on the click of Button should be put within the actionPerformed method.
  • 42. Event Handling  For the event handling we need to create the handler class. The class will be handler class when it implement the appropriate listener and override the methods of interface. Ex:- public class A implements ActionListener { public void actionPerformed(ActionEvent ae) { -------------- -------------- -------------- -------------- } }
  • 43. Event Handling Handler 1:- public class A implements ActionListener { public void actionPerformed(ActionEvent ae) { -------------- -------------- -------------- -------------- } } Handler 2:- public class B implements ActionListener { public void actionPerformed(ActionEvent ae) { -------------- -------------- -------------- -------------- } }
  • 44. Event Handling(ActionEvent)  import java.awt.*;  import java.applet.*;  import java.awt.event.*;  public class MyApp extends Applet  {  public TextField t1,t2,t3;  Button b1;  FlowLayout fl;  public void init()
  • 45. Event Handling(ActionEvent)  {  fl=new FlowLayout( );  setLayout(fl);  t1=new TextField(5);  t2=new TextField(5);  t3=new TextField(5);  b1=new Button("add");  add(t1);  add(t2);  add(t3);  add(b1);
  • 46. Event Handling(ActionEvent)  AddHandler ah=new AddHandler(t1,t2,t3);  b1.addActionListener(ah);   }  }
  • 47. Event Handling(ActionEvent)  import java.awt.*;  import java.applet.*;  import java.awt.event.*;  public class AddHandler implements ActionListener  {  TextField th1,th2,th3;  public AddHandler(TextField t33,TextField t43,TextField t53)  {  th1=t33;  th2=t43;  th3=t53;  }  
  • 48. Event Handling(ActionEvent)  public void actionPerformed(ActionEvent ae)  {   int i=Integer.parseInt(th1.getText());  int j=Integer.parseInt(th2.getText());  String s1=i+j+ "";  th3.setText(s1);  }  }
  • 49. Event Handling  Once the handler is created it should be registered with source without which eventhandling is not possible it is necessary otherwise we don’t know which handler handle the event.  To register the handler with the source th methods are provided in the source.  Registration methods of Button and Textfield source is following . void addActionListener(ActionListner al) // al represent the object of that class which implement the ActionListener interface. Method belonging to Button and Textfield class.
  • 50. Event Handling  Action Event- 1)button is clicked. 2) list item is double clicked. 3)menu item is selected. 4) txt field is entered. ActionListener :- public void actionPerformed(ActionEvent ae)
  • 51. Event Handling  ItemEvent:- 1) checkbox or list item is selected . 2) choice selection is made 3)Checkable menu item is selected or deselected. ItemListener:- public void itemStateChanged(ItemEvent ie)
  • 52. Event Handling  AdjustmentEvent :-Scroll bar is manipulated . AdjustementListener:- void adjustmentValueChanged( AdjustmentEvent e);
  • 53. Event Handling  ComponentEvent:- when a component is hidden moved resized or become visible. ComponentListener:- void ComponentResized(ComponentEvevnt ce); void ComponentMoved(ComponentEvevnt ce); void ComponentShown(ComponentEvevnt ce); void ComponentHidden(ComponentEvevnt ce);
  • 54. Event Handling  FocusEvent :- Component gains or loses keyboard focus.  FocusListener:- void focusGained(FocusEvent fe); void focusLost(FocusLost fl);
  • 55. Event Handling  ContainerEvent :- Component is added or removed from Container . ContainerListener :- void componentAdded(ContainerEvent ce) ; void componentRemoved(ContainerEvent ce )
  • 56. Event Handling  KeyEvent:- Input is received from keyboard .  KeyListener:- void keyPressed(KeyEvent ke); void keyReleased(KeyEvent ke); void keyTyped(KeyEvent ke);
  • 57. Event Handling  MouseEvent :- When mouse is draggd, moved,clicked,pressed or released . MouseListener :- void mouseClicked(MouseEvent me); void mouseEntered(MouseEvent me); void mouseDragged(MouseEvent me);
  • 58. Event Handling void mousePressed(MouseEvent me); void mouseExited(MouseEvent me)
  • 59. Event Handling MouseWheelEvent:- When mouse wheel is moved . MouseWheelListener:- void mouseWheelMoved(MouseWheelEvent mwe );
  • 60. Event Handling  TextEvent:- When the value of text area or text field is generated . TextListener:- void textChanged(TextEvent te);
  • 61. Event Handling  WindowEvent:- When window is activated, closed , deactivated, deiconified , iconified , opened or quit . WindowListener:- void windowActivated(WindowEvent we); void windowClosed(WindowEvent we); void windowClosing(WindowEvent we); void windowIconified(WindowEvent we); void windowDeiconified(WindowEvent we);
  • 62. Event Handling void windowDeactivated(WindowEvent we); void windowOpened(WindowEvent we);
  • 63. Inner class  Classes which is defined within another class. class A { Class B { } }  A is container class or top level class. B is inner class.  One class can contain any no of inner class./Inner classs can contain inner class and this can go upto any level.
  • 64. Inner class  Inner classes are of 4 types. 1)Static inner class 2)Non static inner class 3)Local inner class 4) Anonymous inner class
  • 65. Inner class  Non static inner class:- This class ork as the member of class like static inner class they can have any type of accessibility (public/private/protected/default) ,but like static inner class they can not have static members . Because the non static class is the member of top level class and non static member exist when its object is created .The non static inner class doesn’t exist until the object of top level class created. A non static inner class is always associated within the object of top level class and therefore it has the special privilege it can access any member(static/non static) of its top level class . So that non static inner class is used for event handling.
  • 66. Panel  A Component which is used as a Container. It is used for physically grouping of Component. Constructor:- new Panel(); // Create the panel with Flow layout Manager. new Panel(LayoutManager lm); // Create the panel with same layoutManager by which is pass as the parameter in the panel.
  • 67. Panel  Methods:- add(Component cmp); add(Component cmp,int loc);
  • 68. Standalone application  To cretae a stand alone application we ned to create a class which will inherit Frame class . To run it we don’t need html ,this type of application has it ‘s own window. And this type of application created just like console based application.  Features of stand alone application. 1) Standalone application is also called Frame. 2)Frame provide the outlaw of window we only need to place the components on window. 3)Frame has the same applarcchitecture and structure as the applet and therefore GUI application and the event handling will be same as applet. 4)Like applet it doesn’t have init,start,stop,destroy,paint methods and therefore instead of initialize the applet we need we need to define the constructor of that class which inherit the Frame class. 5)To display standalone applicatio we need main method.just like console based application
  • 69. Standalone application  Import java.awt.*;  Impoort java.applet.*;  Import java.awt.event.*; public class Hobby extends Frame implements ItemListener { --------------- -------------- public Hobbbby( ) { ----------------- ------------------ ------------------ same as init }
  • 70. Standalone application Public void itemStateChanged(ItemListener il) { ---------- ----- ------- } }
  • 71. Standalone application  public class HobbyGui { psvm(String arg[ ]) {Hobby h=new Hobby(); h.setSize(300,300); h.setVisible(true); } }
  • 72. Standalone application  import java.awt.*;  import java.applet.*;  import java.awt.event.*;  class MyFrame extends Frame implements ActionListener  {  TextField t1,t2,t3;  Button b1;  FlowLayout fl;  public MyFrame()  {  fl=new FlowLayout( );  setLayout(fl);  t1=new TextField(5); 
  • 73. Standalone application  t2=new TextField(5);  t3=new TextField(5);  b1=new Button("add");  add(t1);  add(t2);  add(t3);  add(b1);  b1.addActionListener(this);  }
  • 74. Standalone application  public void actionPerformed(ActionEvent ae)  {   int i=Integer.parseInt(t1.getText());  int j=Integer.parseInt(t2.getText());   t3.setText(i+j+"");  }  }
  • 75. Standalone application  public class MyWindow  {  public static void main(String arg[])  {  MyFrame mf=new MyFrame();  mf.setSize(300,300);  mf.setVisible(true);  }  }
  • 76. Standalone application  Some issues on Window:- When w click on crossbar th window doesn’t terminate and still on RAM to close it we have to close the DOS prompt because the event is generated by Frame we have to handle the event of Frame. There are 7 state of Frame when it generate event. In all 7 state it generate the WindowEvent . To handle the 7 state of Frame WindowListener interface provide 7 different methods.When we implement the WindowListener we need to override the all 7 methods. void windowActivated(WindowEvent we); void windowClosed(WindowEvent we); void windowClosing(WindowEvent we);
  • 77. Standalone application void windowIconified(WindowEvent we); void windowDeiconified(WindowEvent we); void windowDeactivated(WindowEvent we); void windowOpened(WindowEvent we);
  • 78. Standalone application 1) windowOpened is called after the application display on the screen . It called only once .It is used to initialize the application. 2)windowIconified is called when window is minimized . 3) windowDeiconified is called when window is maximized . 4) winowActivate is called when the application get the focus. 5)windowDeactvate is called when the application lost the focus. 6)windowClosed is called when we click on cross button . 7)windowClosing dispose():- is called to terminate the application System.exit(0) :-is called to terminate the dos prompt or to terminate the JVM.
  • 79. Standalone application  windowClosing is called when the application is dispose.It is used to save the state of application.This is called only once but closed is not called untill dispose is called .  Registration method :- void addindowListner(WindowEvent we)
  • 80.  import java.awt.*;  import java.applet.*;  import java.awt.event.*;  class MyFrame extends Frame implements ActionListener,WindowListener  {  TextField t1,t2,t3;  Button b1; 
  • 81.  FlowLayout fl;  public MyFrame()  {  fl=new FlowLayout( );  setLayout(fl);  t1=new TextField(5);  t2=new TextField(5);  t3=new TextField(5);
  • 82.  b1=new Button("add");  add(t1);  add(t2);  add(t3);  add(b1);  b1.addActionListener(this);  }
  • 83.  public void actionPerformed(ActionEvent ae)  {   int i=Integer.parseInt(t1.getText());  int j=Integer.parseInt(t2.getText());   t3.setText(i+j+"");  }  public void windowActivated(WindowEvent we) { }  public void windowClosed(WindowEvent we) {dispose(); System.exit(0); }  public void windowClosing(WindowEvent we) { }
  • 84. Standalone application  public void windowIconified(WindowEvent we) { }  public void windowDeiconified(WindowEvent we){ }  public void windowDeactivated(WindowEvent we) { }  public void windowOpened(WindowEvent we) { }  }
  • 85. Standalone application  public class MyWindowHandler  {  public static void main(String arg[])  {  MyFrame mf=new MyFrame();  mf.setSize(300,300);  mf.setVisible(true);  }  }
  • 86. Adapter class WindowListener interface implements UserDefined class
  • 87. Adapter class WindowListener interface implements WindowAdapter class extends UserDefined class
  • 88. Adapter class  import java.awt.*;  import java.applet.*;  import java.awt.event.*;  class MyFrame extends Frame implements ActionListener,WindowAdapter  {  ------------------  ------------------  ---------------------  public void windowClosed(WindowEvent we)  {  dispose();  System.exit(0);  }  ----------------------  ----------------- 
  • 89. Adapter class  ---------------------  }  public class MyWindowAdapter  {  public static void main(String arg[])  {  -------------------  -------------------  ---------------------  }  }
  • 90. Adapter class  All listener those contain more than one methods have Adapter classs. WindowListener . KeyListener MouseListener MouseMotionListener.