Difference between requestFocusInWindow () and grabFocus () in Swing

I would like to know the difference between requestFocusInWindow() and grabFocus() methods. Both of them are great for capturing the focus for me in this program. Therefore, I could not 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(); } }); } } 
+4
source share
1 answer

The answer is simple, grabFocus() captures focus, regardless of whether the top-level ancestor is a focused window. If the window is not active, then it is activated so that the component receives focus.

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

In your example, JDialog is a top-level ancestor, it automatically focuses when you press JButton . Therefore, requestFocusInWindow() and grabFocus() does not matter.

I rewrote 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 does not work (i.e. jt does not get focus) because JDialog activated when JButton pressed and JTextField in JDialog gets focus.

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

+10
source

Source: https://habr.com/ru/post/1491683/


All Articles