Focus events are fired when an element loses the keyboard focus. This is true whether the change in focus comes through the mouse, the keyboard, or programmatically. From the user's point of view, the component with the keyboard focus is usually famous — with a dotted or black border.
Try to understand following code given below which is solve your problem.
Code:
Frame frame = new JFrame("Trial");
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Test Page");
panel.add(button);
frame.getContentPane().add(panel);
frame.pack();
button.requestFocusInWindow();
frame.setVisible(true);
Bookmarks