Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need to set a focus in the JTextField which is added/included in the JPanel

843806Jan 28 2008 — edited Jan 28 2008
Hi,

I have one JTextField in one JPanel and I need to set focus in that JTextField on load of the application.

I have written following code for that:
		for(int index = 0; index < panel.getComponentCount(); index++){
			if(panel.getComponent(index) instanceof JTextField){
				panel.setVisible(true);
				final JTextField t = (JTextField)	panel.getComponent(index);
				//this is not working
				boolean r = t.requestFocusInWindow();
				System.out.println("rend focus: " +r);
				//this is working
				t.setBackground(Color.red);
				break;
			}
}
this.add(panel);
My method returns component having panel which has 4 components and I want to set focus only in the TextField. But above code its not working. Any idea?

Thanks in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 25 2008
Added on Jan 28 2008
2 comments
90 views