Java AWT
Java AWT • Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. • Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. • AWT is heavyweight i.e. its components uses the resources of system. • The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
Java AWT Hierarchy • The hierarchy of Java AWT classes are given below.
• Container • The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel. • Window • The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. • Panel • The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc. • Frame • The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.
Useful Methods of Component class Method Description public void add(Component c) inserts a component on this component. public void setSize(int width,int height) sets the size (width and height) of the component. public void setLayout(LayoutManager m) defines the layout manager for the component. public void setVisible(boolean status) changes the visibility of the component, by default false.
Java AWT Example • To create simple awt example, you need a frame. There are two ways to create a frame in AWT. • By extending Frame class (inheritance) • By creating the object of Frame class (association)
Simple example of AWT by inheritance import java.awt.*; class First extends Frame{ First(){ Button b=new Button("click me"); b.setBounds(30,100,80,30);// setting button position add(b);//adding button into frame setSize(300,300);//frame size 300 width and 300 height setLayout(null);//no layout manager setVisible(true);//now frame will be visible, by defaul t not visible } public static void main(String args[]){ First f=new First(); }}
Simple example of AWT by association import java.awt.*; class First2{ First2(){ Frame f=new Frame(); Button b=new Button("click me"); b.setBounds(30,50,80,30); f.add(b); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public static void main(String args[]){ First2 f=new First2(); }}
Event and Listener (Java Event Handling) • Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. • The java.awt.event package provides many event classes and Listener interfaces for event handling.
Event classes and Listener interfaces: Event Classes Listener Interfaces ActionEvent ActionListener MouseEvent MouseListener and MouseMotionListener MouseWheelEvent MouseWheelListener KeyEvent KeyListener ItemEvent ItemListener TextEvent TextListener AdjustmentEvent AdjustmentListener WindowEvent WindowListener ComponentEvent ComponentListener ContainerEvent ContainerListener FocusEvent FocusListener
Steps to perform Event Handling • Following steps are required to perform event handling: 1. Implement the Listener interface and overrides its methods 2. Register the component with the Listener
For registering the component with the Listener, many classes provide the registration methods. For example: – Button • public void addActionListener(ActionListener a){} – MenuItem • public void addActionListener(ActionListener a){} – TextField • public void addActionListener(ActionListener a){} • public void addTextListener(TextListener a){} – TextArea • public void addTextListener(TextListener a){} – Checkbox • public void addItemListener(ItemListener a){} – Choice • public void addItemListener(ItemListener a){} – List • public void addActionListener(ActionListener a){} • public void addItemListener(ItemListener a){}
EventHandling Codes: • We can put the event handling code into one of the following places: 1. Same class 2. Other class 3. Anonymous class
Example of event handling within class: import java.awt.*; import java.awt.event.*; class AEvent extends Frame implem ents ActionListener{ TextField tf; AEvent(){ tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActionListener(this); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerforme d(ActionEvent e){ tf.setText("Welcome"); } public static void main(St ring args[]){ new AEvent(); } }
Example of event handling by Outer class: import java.awt.*; import java.awt.even t.*; class AEvent2 extend s Frame{ TextField tf; AEvent2(){ tf=new TextField(); tf.setBounds(60,50,1 70,20); Button b=new Button( "click me"); b.setBounds(100,120,80,30); Outer o=new Outer(this); b.addActionListener(o);//passi ng outer class instance add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(String args[]){ new AEvent2(); } }
Cont… import java.awt.event.*; class Outer implements ActionListener{ AEvent2 obj; Outer(AEvent2 obj){ this.obj=obj; } public void actionPerformed(ActionEvent e) { obj.tf.setText("welcome"); } }
Example of event handling by Annonymous class: import java.awt.*; import java.awt.event .*; class AEvent3 extends Frame{ TextField tf; AEvent3(){ tf=new TextField(); tf.setBounds(60,50,17 0,20); Button b=new Button(" click me"); b.setBounds(50,120,80 ,30); b.addActionListener(new Acti onListener(){ public void actionPerformed( ){ tf.setText("hello"); } }); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(Stri ng args[]){ new AEvent3(); } }

object oriented programming examples

  • 1.
  • 2.
    Java AWT • JavaAWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. • Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. • AWT is heavyweight i.e. its components uses the resources of system. • The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
  • 3.
    Java AWT Hierarchy •The hierarchy of Java AWT classes are given below.
  • 4.
    • Container • TheContainer is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel. • Window • The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. • Panel • The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc. • Frame • The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.
  • 5.
    Useful Methods ofComponent class Method Description public void add(Component c) inserts a component on this component. public void setSize(int width,int height) sets the size (width and height) of the component. public void setLayout(LayoutManager m) defines the layout manager for the component. public void setVisible(boolean status) changes the visibility of the component, by default false.
  • 6.
    Java AWT Example •To create simple awt example, you need a frame. There are two ways to create a frame in AWT. • By extending Frame class (inheritance) • By creating the object of Frame class (association)
  • 7.
    Simple example ofAWT by inheritance import java.awt.*; class First extends Frame{ First(){ Button b=new Button("click me"); b.setBounds(30,100,80,30);// setting button position add(b);//adding button into frame setSize(300,300);//frame size 300 width and 300 height setLayout(null);//no layout manager setVisible(true);//now frame will be visible, by defaul t not visible } public static void main(String args[]){ First f=new First(); }}
  • 8.
    Simple example ofAWT by association import java.awt.*; class First2{ First2(){ Frame f=new Frame(); Button b=new Button("click me"); b.setBounds(30,50,80,30); f.add(b); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public static void main(String args[]){ First2 f=new First2(); }}
  • 9.
    Event and Listener(Java Event Handling) • Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. • The java.awt.event package provides many event classes and Listener interfaces for event handling.
  • 10.
    Event classes andListener interfaces: Event Classes Listener Interfaces ActionEvent ActionListener MouseEvent MouseListener and MouseMotionListener MouseWheelEvent MouseWheelListener KeyEvent KeyListener ItemEvent ItemListener TextEvent TextListener AdjustmentEvent AdjustmentListener WindowEvent WindowListener ComponentEvent ComponentListener ContainerEvent ContainerListener FocusEvent FocusListener
  • 11.
    Steps to performEvent Handling • Following steps are required to perform event handling: 1. Implement the Listener interface and overrides its methods 2. Register the component with the Listener
  • 12.
    For registering thecomponent with the Listener, many classes provide the registration methods. For example: – Button • public void addActionListener(ActionListener a){} – MenuItem • public void addActionListener(ActionListener a){} – TextField • public void addActionListener(ActionListener a){} • public void addTextListener(TextListener a){} – TextArea • public void addTextListener(TextListener a){} – Checkbox • public void addItemListener(ItemListener a){} – Choice • public void addItemListener(ItemListener a){} – List • public void addActionListener(ActionListener a){} • public void addItemListener(ItemListener a){}
  • 13.
    EventHandling Codes: • Wecan put the event handling code into one of the following places: 1. Same class 2. Other class 3. Anonymous class
  • 14.
    Example of eventhandling within class: import java.awt.*; import java.awt.event.*; class AEvent extends Frame implem ents ActionListener{ TextField tf; AEvent(){ tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActionListener(this); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerforme d(ActionEvent e){ tf.setText("Welcome"); } public static void main(St ring args[]){ new AEvent(); } }
  • 15.
    Example of eventhandling by Outer class: import java.awt.*; import java.awt.even t.*; class AEvent2 extend s Frame{ TextField tf; AEvent2(){ tf=new TextField(); tf.setBounds(60,50,1 70,20); Button b=new Button( "click me"); b.setBounds(100,120,80,30); Outer o=new Outer(this); b.addActionListener(o);//passi ng outer class instance add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(String args[]){ new AEvent2(); } }
  • 16.
    Cont… import java.awt.event.*; class Outerimplements ActionListener{ AEvent2 obj; Outer(AEvent2 obj){ this.obj=obj; } public void actionPerformed(ActionEvent e) { obj.tf.setText("welcome"); } }
  • 17.
    Example of eventhandling by Annonymous class: import java.awt.*; import java.awt.event .*; class AEvent3 extends Frame{ TextField tf; AEvent3(){ tf=new TextField(); tf.setBounds(60,50,17 0,20); Button b=new Button(" click me"); b.setBounds(50,120,80 ,30); b.addActionListener(new Acti onListener(){ public void actionPerformed( ){ tf.setText("hello"); } }); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(Stri ng args[]){ new AEvent3(); } }