Home Java Example Java Awt Handling Focus Changes in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Handling Focus Changes in Java

Advertisement
This section provides an example that handles focus changes in java.

Handling Focus Changes in Java

     

Introduction

In this section, you will learn about handling the focus changes in java. This section shows you how the event be handled according to the focuses and to find out whether the component got the focus or lost the focus.  

This program determines the focus changes events. You can set the different-different events for the focused of the several components particularly. The generated focus events are performed by the FocusListener of the object used in your application using the addFocusListener() method. The generated event (FocusEvent) is passed to every FocusListener objects that receives such types of events using the addFocusListener() method of the object. The addFocusListener() method is takes the instance of MyFcousListener class.

MyFocusListener:
This is the inner class used in the FocusChange class in which, the focusGained() method has been used to receive the generated event. This method sets the text of the source of the event to the label component. This program displays the three command buttons on the frame. If you select any button of those then the text of that button will be shown on the label.

FocusAdapter:
This is the abstract class is used to receive the keyboard focus event. 

Here is the code of program:

import java.awt.*;
import java.awt.event.*;

public class FocusChange{
  Label label;
  public static void main(String[] args){
  FocusChange fc = new FocusChange();
  }

  public FocusChange(){
  Frame frame = new Frame("RoseIndia.Net");
  Panel panel = new Panel();
  Button yes = new Button("Yes");
  Button no = new Button("No");
  Button cancel = new Button("Cancel");
  yes.addFocusListener(new MyFocusListener());
  no.addFocusListener(new MyFocusListener());
  cancel.addFocusListener(new MyFocusListener());
  panel.add(yes);
  panel.add(no);
  panel.add(cancel);
  frame.add(panel,BorderLayout.NORTH);
  label = new Label();
  frame.add(label,BorderLayout.CENTER);
  frame.setSize(400,400);
  frame.setVisible(true);
  frame.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });
  }

  public class MyFocusListener extends FocusAdapter{
  public void focusGained(FocusEvent fe){
  Button bt = (Button)fe.getSource();
  String str = bt.getLabel();
  label.setText(str);
  }
  }
}

 

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Ask Questions?    Discuss: Handling Focus Changes in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
http://www.roseindia.net/Captcha.jpg
April 11, 2012
http://www.roseindia.net/Captcha.jpg

http://www.roseindia.net/Captcha.jpg http://www.roseindia.net/Captcha.jpg
Arvinder singh
April 24, 2012
java help

pls my help in java programming
DMCA.com