1. Computing

A FocusListener Example Program

By

Java Code Listing

The following Java program creates a simple GUI containing a number of graphical components - a JButton, JLabel, JComboBox, JTextField all placed in a JPanel and a JTextArea placed in a JScrollPane. The purpose of the graphical components are to be able to gain and lose the focus as a user clicks upon them. As the focus changes the FocusListener implemented will provide information about the focus event which is appended to the JTextArea.

The FocusListener interface could have been implemented in a separate class but it's just as easy to use the containing class. This enables all the graphical components to attach to the same FocusListener.

The article that goes with this program listing is Implementing a FocusListener.

import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JScrollPane;

//The FocusListenerExample class implements the FocusListener interface
public class FocusListenerExample implements FocusListener {
    
    JTextArea exTextArea;

     //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
     

         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
         {
             
            @Override
             public void run()
             {
                 
                 new FocusListenerExample().BuildGUI();         
             }
         });
              
    }
    
    public void BuildGUI()
    {
        JFrame guiFrame = new JFrame();
        
        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Creating a Table Example");
        guiFrame.setSize(700,200);
      
        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);
        
        //A JPanel to hold some components to make the layout easy.
        JPanel graphComps = new JPanel();
        
        //A JButton attached to the FocusListener
        JButton exButton = new JButton("Example Button");
        exButton.setActionCommand("Example Button");
        exButton.addFocusListener(this);
        
        //A JCombo attached to the FocusListener
        String[] items = {"London", "Paris", "Madrid", "Moscow", "Wellington"};
        JComboBox exCombo = new JComboBox(items);
        exCombo.addFocusListener(this);
        
        JLabel exLabel = new JLabel("Graphical Components:");

        //A JTextField attached to the FocusListener
        JTextField exTextField = new JTextField("A Text Field");
        exTextField.addFocusListener(this);
        
        //A JTextArea attached to the FocusListener. It's put in 
        //a JScrollPane to be able to handle all the events created.
        exTextArea = new JTextArea("Event Feedback..");
        exTextArea.addFocusListener(this);
        JScrollPane scrollText = new JScrollPane(exTextArea);
        
        //Place all the components in the JFrame
        graphComps.add(exLabel);
        graphComps.add(exCombo);
        graphComps.add(exTextField);
        graphComps.add(exButton);
        guiFrame.add(graphComps,BorderLayout.NORTH);
        guiFrame.add(scrollText, BorderLayout.CENTER);
        
        guiFrame.setVisible(true);
        
    }
    
    //The FocusListener methods append information about
    //the focus events to the JTextArea
    @Override
    public void focusGained(FocusEvent e) {
        exTextArea.append(e.getComponent().getClass().getName() + " has got the focus\n");
    }
    
    @Override
    public void focusLost(FocusEvent e) {
        exTextArea.append(e.getComponent().getClass().getName() + " has lost the focus\n");
    }
}
  1. About.com
  2. Computing
  3. Java
  4. Create User Interfaces
  5. Handling Events
  6. A FocusListener Example Program

©2014 About.com. All rights reserved.