FocusListener in Java Swing

Focus Listener is the interface which is used to receive the keyboard focus event.focusGained(gains the keyboard focus) and focusLost(loses the keyboard focus) are the two methods.

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

/* <applet

     code = "Visible.class"

     height = 300

     width = 300

   >

   </applet>

   */

public class Visible extends JApplet implements FocusListener

{

                JPanel panel;

                JTextField t1,t2;

                JButton b1=new JButton("b1");

                

                GridLayout grl=new GridLayout(2,2,15,15);//set the grid(rows and columns)

                public void init()

                {

                                panel=new JPanel();

                                getContentPane().add(panel);

                                panel.setLayout(grl);

                                t1=new JTextField(10);

                                t2=new JTextField(10);

                                t1.setText("Text1");

                                t2.setText("Text2");

                                panel.add(t1);

                                panel.add(t2);

                                t2.setVisible(false);

                                panel.add(b1);

                                b1.setToolTipText("pls click on this button");

                                t1.addFocusListener(this);

                                t2.addFocusListener(this);

                }

                public void focusGained(FocusEvent fe)

                {

                if((JTextField)fe.getSource()==t2)

                                {

                                                t1.setVisible(true);

                                                t2.setVisible(false);

                                }

                }

                public void focusLost(FocusEvent fe)

                {

                                if((JTextField)fe.getSource()==t1)

                                {

                                                t2.setVisible(true);

                                                t1.setVisible(false);

                                }

                                               

                }

}

FocusListener in Java Swing

Explanation....

GridLayout is a Layout manager that lays out a container’s components in a rectangular(two sides are equal) grid(rows&col). The container(special type of component that can hold other components .for example JPanel,JFrame) is divided into equal sized rectangles, and one component(Button,TextField) is placed in each rectangle. JPanel is a supporting container that cannot be displayed on its own but must be added to another container. Panel can be added to the content pane of a Frame. They help to organize the components in a GUI.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @