Event handling in Java AWT



Introduction
In this section, you will learn how to handle events in java awt. Here, this is done through the java.awt.*; package of java. Events are the integral part of the java platform. You can see the concepts related to the event handling through this example and used methods through which you can implement the event driven application.

JAVA


This example shows you how to handle events in java.awt.*; package. AwtEvent is the main class of the program which extends from the Frame class implements the ActionListener interface.
This program starts to run from the main method in which the object for the AwtEvent class has been created. The constructor of the AwtEvent class creates two buttons with adding the addActionListener() method to it. The constructor also initializes a label with text "Roseindia.net".
When you click on the button then the actionPerformed() method is called which receives the generated event. This method shows the text of the source of the event on the label.
Here is the code of the program : 
import java.awt.*;
import java.awt.event.*;

public class AwtEvent extends Frame implements ActionListener{
  Label lbl;
  public static void main(String argv[]){
  AwtEvent t = new AwtEvent();
  }
  
  public AwtEvent(){
  super("Event in Java awt");
  setLayout(new BorderLayout());
  try{
  Button button = new Button("INSERT_AN_URL_HERE");
  button.addActionListener(this);
  add(button, BorderLayout.NORTH);
  Button button1 = new Button("INSERT_A_FILENAME_HERE");
  button1.addActionListener(this);
  add(button1, BorderLayout.SOUTH);
  lbl = new Label("Roseindia.net");
  add(lbl, BorderLayout.CENTER);
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });
  }
  catch (Exception e){}
  setSize(400,400);
  setVisible(true);
  }
  
  public void actionPerformed(ActionEvent e){
  Button bt = (Button)e.getSource();
  String str = bt.getLabel();
  lbl.setText(str);
  }
}


Handling Key Press Event in Java
Posted on: April 17, 2007 at 12:00 AM
In this section you will learn about how to handle key press event in java.
Handling Key Press Event in Java
http://www.roseindia.net/images/previous.gif    http://www.roseindia.net/images/bt_home.gif  http://www.roseindia.net/images/next.gif
Introduction
In this section, you will learn about the handling key press event in java. Key Press is the event is generated when you press any key to the specific component. This event is performed by the KeyListener. When you press or release keys for the writing purpose then key events are fired by the KeyListener objects which generates the KeyEvent environment for the component.
In this program, you will see how operations are performed when you press the key to the specific component. Here, the KeyPress is a constructor of the main class. This constructor creates a frame and set text field to the panel. Then the panel and the label have been set to the frame in the program. When you enter the character in the text field through the keyboard then your entered data will be displayed in the label. There are various method have been used to do the required are given : 
getKeyChar():
This is the method of the KeyEvent class which determines the character that has been entered by you. This method returns the character associated the KeyEvent.
KeyAdapter
This is the class is used to receive the keyboard events. It creates the keyListener objects using the addKeyListener() method. The generated event is passed to every KeyListener objects that receives such types of events using the addKeyListener() method of the object.
KeyPressed():
This method has been used in the program which receives the generated event when you press any key to the object. Above method also sets the text of the source of the event to the label.
Here is the code of program:
import java.awt.*;
import java.awt.event.*;

public class KeyPress extends Frame{
  Label label;
  TextField txtField;
  public static void main(String[] args) {
  KeyPress k = new KeyPress();
  }

  public KeyPress(){
  super("Key Press Event Frame");
  Panel panel = new Panel();
  label = new Label();
  txtField = new TextField(20);
  txtField.addKeyListener(new MyKeyListener());
  add(label, BorderLayout.NORTH);
  panel.add(txtField, BorderLayout.CENTER);
  add(panel, BorderLayout.CENTER);
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });
  setSize(400,400);
  setVisible(true);
  }

  public class MyKeyListener extends KeyAdapter{
  public void keyPressed(KeyEvent ke){
  char i = ke.getKeyChar();
  String str = Character.toString(i);
  label.setText(str);
  }
  }  
}


Item Events in Java
Posted on: April 17, 2007 at 12:00 AM
In this section, you will learn about handling item events in java.
Item Events in Java
http://www.roseindia.net/images/previous.gif    http://www.roseindia.net/images/bt_home.gif  http://www.roseindia.net/images/next.gif
Introduction
In this section, you will learn about handling item events in java. This demonstrates that the event generated when you select an item from the group of the items.
This program demonstrates how the item events is handled. You can perform several operations according to the selecting of  items from the item group.
Here, you will see the handling item event through the given program in which, a combo box and a text area have been taken. Items of the combo box are as follows : Red, Green, Blue. If you select an item from the combo box then the message with the item name will be displayed in the text area.
Choice() :
This is the constructor of the Choice class which creates combo box.
ItemEvent :
This is the ItemEvent class which indicates  an event on selecting or deselecting items from the item group. This event is passed by ItemListener object. The generated event is passed to all ItemListener objects which is registered to receive such types of event. This is done using the addItemListener() method of the object.
getItem() :
This is the method of the ItemEvent class which returns the value of the selected or deselected item.
Here is  the code of program :
import java.awt.*;
import java.awt.event.*;

public class AwtItemEvent extends Frame{
  TextArea txtArea;
  public AwtItemEvent(String title){
  super(title);
  txtArea = new TextArea();
  add(txtArea, BorderLayout.CENTER);
  Choice choice = new Choice();
  choice.addItem("red");
  choice.addItem("green");
  choice.addItem("blue");
  choice.addItemListener(new ItemListener(){
  public void itemStateChanged(ItemEvent e){
  txtArea.setText("This is the " + e.getItem() + " color.\n");
  }
  });
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  System.exit(0);
  }
  });
  add(choice, BorderLayout.NORTH);
  setSize(400,400);
  setVisible(true);
  setResizable(false);
  }
  
  public static void main(String[] args){
  AwtItemEvent f = new AwtItemEvent("AWT Demo");
  }
}


How to use KeyListener
Posted on: April 17, 2007 at 12:00 AM
This section provides an example that implements keyListener in java.
How to use KeyListener 
http://www.roseindia.net/images/previous.gif    http://www.roseindia.net/images/bt_home.gif  http://www.roseindia.net/images/next.gif
Introduction 
In this section, you will learn how to handle different key events on the Java Awt component by using the event handling in Java. When the given program is executed then you will see the several key events these are applied on the component like the key pressed, key release and so on.
All the key events are handled through the KeyListener Interface that has been implemented in the main class of the program.
Description of this program:
In this program, you will see how to show display the specific massage on the frame according to the event. When you press the key then the message "Key Pressed" will be seen on the frame and if when you stop pressing keys then the message "Key Released" will be seen.
This is possible by using the KeyListener interface. It will generate the KeyEvent and you can the check the exact event by using different method like the keyTyped(), keyPressed() and the keyReleased() method which are used for retrieving the generated specific event.
Here is the code of this program:
import java.awt.*; 
import java.awt.event.*;
 
public class KeyListenerTester extends Frame implements KeyListener{  
  TextField t1;
  Label l1;
  public KeyListenerTester(String s ) {  
  super(s); 
  Panel p =new Panel();
  l1 = new Label ("Key Listener!" ) ;
  p.add(l1);  
  add(p);
  addKeyListener ( this ) ; 
  setSize ( 200,100 );
  setVisible(true);
  addWindowListener(new WindowAdapter(){
 public void windowClosing(WindowEvent e){
  System.exit(0);
 }
  });
  }  
  public void keyTyped ( KeyEvent e ){  
  l1.setText("Key Typed");
 }  
  public void keyPressed ( KeyEvent e){  
  l1.setText ( "Key Pressed" ) ; 
  }  
  public void keyReleased ( KeyEvent e ){  
  l1.setText( "Key Released" ) ; 
  }  
  public static void main (String[]args ){  
  new KeyListenerTester ( "Key Listener Tester" ) ; 
 }  
  }  
  
Output of program:
http://www.roseindia.net/java/example/java/awt/keyListener.gif


Simple Form in Java
Posted on: April 17, 2007 at 12:00 AM
This section describes to create a simple form in java.
Simple Form in Java
http://www.roseindia.net/images/previous.gif    http://www.roseindia.net/images/bt_home.gif  http://www.roseindia.net/images/next.gif
This is a simple program of java awt package which constructs a look like a form by using various java component. In this section, you will learn how to create it. This is done by using java gui programming using Classes or APIs of java awt package.
Program Description:
Here, all the components like text boxes and command button have been created on the Java Awt Frame. There are multiple classes have been used in the program for creating specific component like text boxes, labels, command button, panel (a container that holds all the component on the frame). These are explained as follows:
Panel: This is the class of Java Awt package that is used for creating a container.
Label: This is the class of Java Awt package that is used for creating Label component labeled with string like "First Name" and another is the "Last Name".
Here is the code of this program:
import java.awt.*;
import java.awt.event.*;

public class DataEntry {
  public static void main(String[] args) {
  Frame frm=new Frame("DataEntry frame");
  Label lbl = new Label("Please fill this blank:");
  frm.add(lbl);
  frm.setSize(350,200);
  frm.setVisible(true);
  frm.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  System.exit(0);
  }
  });
  Panel p = new Panel();
  Panel p1 = new Panel();
  Label jFirstName = new Label("First Name");
  TextField lFirstName = new TextField(20);
  Label jLastName =new Label("Last Name");
  TextField lLastName=new TextField(20);
  p.setLayout(new GridLayout(3,1));
  p.add(jFirstName);
  p.add(lFirstName);
  p.add(jLastName);
  p.add(lLastName);
  Button Submit=new Button("Submit");
  p.add(Submit);
  p1.add(p);
  frm.add(p1,BorderLayout.NORTH);
  }
}
Output this program:
http://www.roseindia.net/java/example/java/awt/dataentry.gif
Hiding Frame in Java
Posted on: March 18, 2008 at 12:00 AM
This section illustrates you how to hide the Java Awt frame. Here, you have seen in the given following example that provides you the complete code of the program.
Hiding Frame in Java
http://www.roseindia.net/images/previous.gif    http://www.roseindia.net/images/bt_home.gif  http://www.roseindia.net/images/next.gif
Introduction
This section illustrates you how to hide the Java Awt frame. Here, you have seen in the given following example that provides you the complete code of the program.
Description of  program:
In this program, program you will see that only one method of the frame is important for the purpose. The following program show a frame titled with "Frame Hiding". When you click on the close button of the frame, the frame will not be closed but it will hide certain with holding all frame data as it is.
In this program, program we are handling the event generated by the frame when you click on the close button of the frame. By using the getSource() method of the WindowEvent class we are creating the Frame object and frame hides when you call the setVisible() method by passing a boolean valued argument.
Method's Description:
getSource(): This is the method of EventObject class. Hence, here this method is used for the WindowEvent class so, it can return the Form's object that is done invisible by using the setVisible() method. The getSource() method returns the source of the generated event.
setVisible(false): This is the method of Form class i.e. used for setting the form whether visible or not. This method takes a boolean valued argument which decides the form for visible or not. If you pass the boolean value false then the form will be invisible otherwise if you argument is true then the form object will be visible. By default Java Awt Form is in the invisible state. For showing the form you have to set the form visible by using the setVisible(true) method.
Here is the code of program:
import java.awt.*;
import java.awt.event.*;

public class  FrameHide{
  public static void main(String[] args){
  Frame fa= new Frame("Frame Hiding");
  Panel p =new Panel();
  Label l1=new Label("Welcome roseindia");
  p.add(l1);
  fa.add(p);
  fa.setSize(300,200);
  fa.setVisible(true);
  fa.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  Frame frame = (Frame)e.getSource();
  frame.setVisible(false);
 }
  });
 }  
}
Output of this Program:
 http://www.roseindia.net/java/example/java/awt/frame-1.gif
Handling Mouse Clicks in Java
Posted on: April 17, 2007 at 12:00 AM
In this section, you will learn about handling the mouse click event in the awt application.
Handling Mouse Clicks in Java
http://www.roseindia.net/images/previous.gif    http://www.roseindia.net/images/bt_home.gif  http://www.roseindia.net/images/next.gif
Introduction
In this section, you will learn about handling the mouse click event in the awt application.
This program simply implements the left click event of the mouse. When you click "Click Me" button then the text of the label will change from "Roseindia.net" to the text of the generated event source and vice versa.
Following are some methods used in the program are given : 
MouseClick() :
This is the constructor of MouseClick class. In which, buttons are set on the frame and event listeners are attached to the button to process the clicked (pressed) event.
setText() :
This method has been used to set the text of the event source to the label. Syntax : setText(String).
equals() :
This is the equals() method of the String class is used to compare strings. It returns boolean value either true or false.
Here is the code of the program : 
import java.awt.*;
import java.awt.event.*;

public class MouseClick {
  Label lbl;
  public static void main(String[] args) {
  MouseClick MC = new MouseClick();
  }

  public MouseClick(){
  Frame f = new Frame("Checking the mouse click");
  Panel p = new Panel();
  Button button = new Button("Click Me");
  button.addMouseListener(new MyMouseListener());
  p.add(button, BorderLayout.NORTH);
  f.add(p,BorderLayout.NORTH);
  lbl = new Label("Roseindia.net");
  f.add(lbl, BorderLayout.CENTER);
  f.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });
  f.setSize(400,400);
  f.setVisible(true);
  }
  
  public class MyMouseListener extends MouseAdapter{
  public void mouseClicked(MouseEvent me){
  String str = lbl.getText();
  if (str.equals("Roseindia.net")){
  lbl.setText("You have clicke the button.");
  }
  else if (str.equals("You have clicke the button.")){
  lbl.setText("Roseindia.net");
  }
  }
  }
}


Post a Comment

0 Comments