Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

how to use method requestFocus()  RSS feed

Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very simple program to test use method requestFocus()
my program is below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class TextTestFrame extends JFrame implements FocusListener
{ public TextTestFrame()
{ setTitle("TextTest");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);

Container contentPane = getContentPane();
JPanel p = new JPanel();
hourField = new JTextField("12", 3);
minuteField = new JTextField("00", 3);
contentPane.add(p, "South");
public void focusLost(FocusEvent evt)
public void focusGained(FocusEvent evt)
private JTextField hourField;
private JTextField minuteField;
public class TextTest
{ public static void main(String[] args)
{ JFrame frame = new TextTestFrame();;
I only want when I run this program , the focus at minuteField . but it always focus at hourField .
What’s wrong with me ? how to do it ?
Please answer my question detailly!
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, look at this:

1. If you dont�t want to handel FocusEvents you don�t need a FocusListener.
2. The requestFocus() method must be called after the frame is set visible.
Regards, Dirk
PS: No offence, the way you format your code - it�s sort of weird.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!