• Post Reply Bookmark Topic Watch Topic
  • New Topic

setting focus on a component

 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
I have a JTextArea and a JTextField . The JTextArea come first and the JTextField comes after it. Now the first component ( that is the JTextArea ) has focus . Had the JTextField been the first component , it would have also got focus on the load of the JFRAme .
How can i set the focus on a specific component when the JFrame is loaded?
please if you help me
Thanks in advance
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raghav
How can i set the focus on a specific component when the JFrame is loaded?

Did you try to
componentName.requestFocus();
when the JFrame is loaded?
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
i wrote this line of code when i ceated the contructor for the JTextField .
Anything else do i need to do ?
Thanks in advance
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raghav
humm....
Did you created any JPanel to store the components?
Or just used the JFrame to store these two components?
I suggest you can try to requestFocus on the component which store the other subcomponents.
In your case, request focus on JFrame.
I am not sure but I can go to school then find out the assignment which component need to request focus.
Because I did a assignment caused the same effect as your case but I eventually solved the problem.
Please wait one more day
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure ....i can wait but please be a bit fast
Thanks a ton in advance
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raghav
I find out the result is the same as previous post.I write the requestFocus on the JPanel which contains all the movable components.
And I have asked my classmates who give me the same answer.
But a classmate give a special method you can try it if the previous answer can not work.
If you have not the MouseListener,add it and use the mousePressed event handler/method which you add the requestFocus method in it.So each time you click the mouse,it will get focus on the component which you want to point at.
This idea seems to work fine
But I without try it.
I hope that the previous ones or final ones could help you solve the problem.
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing i would like you to know that both these components ( JTextArea and JTextField) are added on the same Panel .
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oo...
Did you try requestFocus on the panel where you can paste the requestFocus() on this constructor?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!