Implementing a FocusListener Interface
Sometimes it's useful to find out which component on a graphical user interface has the focus. For example, it could trigger a change in the GUI to enhance the interaction with the user. One way to achieve this is to implement the
FocusListener
interface to listen for focus events.The FocusListener Interface
To use the
FocusListener
interface a class needs to implement two methods:public interface FocusListener{public void focusGained(FocusEvent e);public void focusLost(FocusEvent e);}
The
focusGained
method will be called when a graphical component gets the focus (i.e., the user clicks on it or tabs to it). The focusLost
method will be called as a graphical component loses the focus.The FocusEvent Object
The
FocusEvent
object is passed to the FocusListener
methods to provide information about the event that has been triggered - such as the component in question. The FocusEvent
object contains useful methods that are commonly used:isTemporary
- returns aboolean
if gaining or losing the focus is temporary.getOppositeComponent
- returns a reference to the component that is on the other end of the focus change.getComponent
- returns a reference to the component on the receiving end of the focus change.
Use the
FocusEvent
object to piece together the details of the Focus Event.Implementing the FocusListener
To implement the
FocusListener
interface we need some graphical components in a GUI to be able to lose and gain the focus. The following code is an example of a simple GUI which does nothing more than place some graphical components in a JFrame
: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;public class FocusListenerExample {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 closesguiFrame.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 screenguiFrame.setLocationRelativeTo(null);JPanel graphComps = new JPanel();JButton exButton = new JButton("Example Button");exButton.setActionCommand("Example Button");String[] items = {"London", "Paris", "Madrid", "Moscow", "Wellington"};JComboBox exCombo = new JComboBox(items);JLabel exLabel = new JLabel("Graphical Components:");JTextField exTextField = new JTextField("A Text Field");exTextArea = new JTextArea("Event Feedback..");graphComps.add(exLabel);graphComps.add(exCombo);graphComps.add(exTextField);graphComps.add(exButton);guiFrame.add(graphComps,BorderLayout.NORTH);guiFrame.add(exTextArea, BorderLayout.SOUTH);guiFrame.setVisible(true);}}
Now let's add the
FocusListener
. First, add the import statements to top of the class:import java.awt.event.FocusListener;import java.awt.event.FocusEvent;
Next we need to implement the
FocusListener
interface. In this example the program will be listening for focus changes across most of the graphical components on the JFrame
. It makes sense for the JFrame
class to implement the FocusListener
interface so that all the components can use it. So, change the class definition to be:public class FocusListenerExample implements FocusListener {
This means we have to add the methods of the
FocusListener
interface to the FocusListenerExample
class. Add the following methods after the BuildGUI
method:@Overridepublic void focusGained(FocusEvent e) {exTextArea.append(e.getComponent().getClass().getName() + " has got the focus\n");}@Overridepublic void focusLost(FocusEvent e) {exTextArea.append(e.getComponent().getClass().getName() + " has lost the focus\n");}
These methods are just getting information about the component at the heart of the focus event and appending that information to the
exTextArea
. The FocusListenerExample
class is now implementing the FocusListener
interface and is ready to receive focus events. All that's left to do is point the graphical components to the FocusListener
. This can be done by using the addFocusListener
method for each component. Add these lines to the BuildGUI
method:exButton.addFocusListener(this);exCombo.addFocusListener(this);exTextField.addFocusListener(this);exTextArea.addFocusListener(this);
Now when the
exButton
, exCombo
, exTextField
and exTextArea
get or lose the focus a focus event will be triggered. Notice how the exLabel
JLabel
does not have a FocusListener
attached - it's not often that a JLabel
would want to have the focus.When the program is executed the focus can be shifted from component to component by clicking on each one. As this happens the
JTextArea
at the bottom will display information about the focus events being triggered.The full Java code listing can be found in A FocusListener Example Program.