Monday, 31 March 2014

FocusListener



public interface FocusListener extends EventListener {
    public void focusGained(FocusEvent e);
    public void focusLost(FocusEvent e);
}

The listener interface for receiving keyboard focus events on a component. When the component gains or loses the keyboard focus, the relevant method in the listener object is invoked, and the FocusEvent is passed to it.


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

class FocusListenerInterfEx implements FocusListener{

 public void focusGained(FocusEvent e){
  System.out.println("Focus Added");
 }
 
 public void focusLost(FocusEvent e){
  System.out.println("Focus Removed");
 }
}


import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;

class FocusListenerEx{
 public static void main(String args[])throws Exception{
  JFrame myFrame = new JFrame("Event Handling Example");
  JButton button1 = new JButton("Button 1");
  FocusListenerInterfEx obj1 = new FocusListenerInterfEx();
  int count = 0;
  
  myFrame.setVisible(true);
  myFrame.setSize(250, 250);
  
  String str;
  
  /* Register Focus Lister */
  button1.addFocusListener(obj1);
  
  /* Add Buttons to Button */
  myFrame.add(button1);
  
 }
}

Output






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment