Forums Register Login

how can i set the focus?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
requestFocus() should work. Can you show us some code?
+Pie Number of slices to send: Send
call requestFocus after making your frame visible.
frame.setVisible(true);
secondTextField.requestFocus();
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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();
}
}
}

+Pie Number of slices to send: Send
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 need a new interior decorator. This tiny ad just painted every room in my house purple.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1044 times.
Similar Threads
Plz help! Not able to get focus on text field!!
[Javascript] Focus Textfield on Page Load
how to set focus?
JScrollPane problem
to get default focus
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:55:29.