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
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(); } } }
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).]
Post by:autobot
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