aspose file tools*
The moose likes Beginning Java and the fly likes how can i set the focus? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can i set the focus?" Watch "how can i set the focus?" New topic
Author

how can i set the focus?

Pradeep Kanwar
Ranch Hand

Joined: Jun 11, 2001
Posts: 38
hi,
i have 2 text field in a panel in a frame.
i want the focus (i should be able to type) to be on second (added later) textfield.
how can i do this? i have used requestFocus() method on textField, but it doesnt work.
plz help
thanks in advance.
pradi
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
requestFocus() should work. Can you show us some code?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Prerna Bansal
Greenhorn

Joined: May 01, 2001
Posts: 8
call requestFocus after making your frame visible.
frame.setVisible(true);
secondTextField.requestFocus();
Pradeep Kanwar
Ranch Hand

Joined: Jun 11, 2001
Posts: 38
Thanks but it still doesn't work.
Here is the code: i want the focus to be on the second text field (fibTextField2)

Thanks in advance!
regards
pradi
(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited September 12, 2001).]
Pradeep Kanwar
Ranch Hand

Joined: Jun 11, 2001
Posts: 38
I got the solution but its crude one. Would be great to find alternatives.
This is from one of the java tips and tricks site http://privat.schlund.de/b/bossung/prog/java/tips.html
Problem: Initially giving the focus to a specified component in the dialog.
Version: Swing 1.x
Suggested Solution: Swing will initially give the focus to the first component you add to the dialog. Thus you can just add the desired component first. If that does not work for you it seems you have to go through major pain.
There is of course a method in each component that requests the focus (therefore called requestFocus()) which works fine. However, if you call it during your initializing operations, Swing will late give the focus back to the first component you added. The only way around this, which I found working more than one system, is to write a class that will focus a component about 0.5 seconds after it is initialized:
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class FocusSetter implements ActionListener {
private JComponent component;
private javax.swing.Timer timer;
public FocusSetter(JComponent comp) {
this.component = comp;
this.timer = new javax.swing.Timer(500, this);
this.timer.start();
}
public void actionPerformed(ActionEvent evt) {
if ((evt != null) && (evt.getSource() == timer)) {
component.requestFocus();
}
}
}

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
This solution works by using requestDefaultFocus() and putting the call in windowOpened(). Of course for that to work, you need to make the call in a "window" of some sort, so I had to extend JFrame instead.
I am not sure what you were trying to accomplish by having it be a Panel, so I don't know if this is a workable solution for you.

[This message has been edited by Cindy Glass (edited September 12, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i set the focus?