Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between requestFocusInWindow() and grabFocus() in Swing

Tags:

java

focus

swing

I would like to know the difference between requestFocusInWindow() and grabFocus() methods. Both of them work fine for grabbing the focus for me in this program. Therefore, i couldn't understand the difference.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Focus extends JFrame
{
JButton jb;

    public Focus()
    {
        createAndShowGUI();
    }

    private void createAndShowGUI()
    {
        setTitle("grabFocus() vs requestFocusInWindow()");
        setLayout(new FlowLayout());
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jb=new JButton("Open Dialog");
        jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                showDialog();
            }
        });

        add(jb);
    }

    private void showDialog()
    {
        JDialog jd=new JDialog();
        jd.setLayout(new GridLayout(2,2));
        jd.setVisible(true);


        JLabel l1=new JLabel("Label 1");
        JLabel l2=new JLabel("Label 2");

        JTextField jt1=new JTextField(20);
        JTextField jt2=new JTextField(20);

        jd.add(l1);
        jd.add(jt1);
        jd.add(l2);
        jd.add(jt2);

        // Both of them are doing the thing
        //jt2.grabFocus();
        jt2.requestFocus();

        jd.pack();
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
                new Focus();
            }
        });
    }
}
like image 672
JavaTechnical Avatar asked Jul 16 '13 15:07

JavaTechnical


People also ask

What is requestFocusInWindow?

Whereas, requestFocusInWindow() gets the focus for the component on which it is called only when its top-level ancestor is the focused window. In your example, JDialog is the top level ancestor, it gets focus automatically when the JButton is clicked.

What is the use of JComponent class in Swing?

The class JComponent is the base class for all Swing components except top-level containers. To use a component that inherits from JComponent, you must place the component in a containment hierarchy whose root is a top-level SWING container.

What does setFocusable do in Java?

You can use setFocusable(boolean n) , it´s mainly used to activate or deactivate the focus event (component of the graphical user interface that is selected to receive the input) of the view, both in the tactile / mouse mode, and in the keyboard (cursor) mode.

How do I set preferred size in Java?

Generally, setPreferredSize() will lay out the components as expected if a layout manager is present; most layout managers work by getting the preferred (as well as minimum and maximum) sizes of their components, then using setSize() and setLocation() to position those components according to the layout's rules.


1 Answers

The answer is simple, grabFocus() grabs the focus, no matter whether the top-level ancestor is the focused window. If the window is not active, then it is made active to let the component get the focus.

Whereas, requestFocusInWindow() gets the focus for the component on which it is called only when its top-level ancestor is the focused window.

In your example, JDialog is the top level ancestor, it gets focus automatically when the JButton is clicked. So requestFocusInWindow() and grabFocus() does not make a difference.

I have re-written the program to better understand the difference using a pragmatic approach.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Focus extends JFrame
{
JButton jb;
JTextField jt;

    public Focus()
    {
        createAndShowGUI();
    }

    private void createAndShowGUI()
    {
        setTitle("grabFocus() vs requestFocusInWindow()");
        setLayout(new FlowLayout());
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jb=new JButton("Open Dialog");
        jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                showDialog();
            }
        });

        add(jb);

        jt=new JTextField(20);

        add(jt);
    }

    private void showDialog()
    {
        JDialog jd=new JDialog();
        jd.setLayout(new GridLayout(2,2));
        jd.setVisible(true);


        JLabel l1=new JLabel("Label 1");
        JLabel l2=new JLabel("Label 2");

        JTextField jt1=new JTextField(20);
        JTextField jt2=new JTextField(20);

        jd.add(l1);
        jd.add(jt1);
        jd.add(l2);
        jd.add(jt2);

        jt.requestFocusInWindow();
        //jt.grabFocus();

        jd.pack();
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
                new Focus();
            }
        });
    }
}

Here, requestFocusInWindow() is called on jt and it doesn't work (i.e. jt doesn't get the focus) because the JDialog is made active when the JButton is clicked and JTextField in the JDialog gets the focus.

Next, grabFocus() works. When the JButton is clicked, JDialog is displayed, but will not be active. Because upon call to the grabFocus(), immediately the JFrame becomes the active top-level ancestor and jt is forced to get the focus.

like image 169
JavaTechnical Avatar answered Nov 10 '22 03:11

JavaTechnical