heng03
asked on
How to set Focus for Jcomponent such as JTextField, JRadioButton etc
Hi
How to set Focus for JComponet such as
JTextField, JRadioButton, etc.
That is, when displaying the Panel or JFrame, the Jcomponent should be the
default focus field. (no need to click
onto it to get focus).
How to set Focus for JComponet such as
JTextField, JRadioButton, etc.
That is, when displaying the Panel or JFrame, the Jcomponent should be the
default focus field. (no need to click
onto it to get focus).
ASKER
I have try it out, it doesn't work.
Pls note that I used JPanel to
Layout these JComponets.
Sometime, Panel within Panel.
Any probelm to set focus for such layout ?
Thanks
Phua
Pls note that I used JPanel to
Layout these JComponets.
Sometime, Panel within Panel.
Any probelm to set focus for such layout ?
Thanks
Phua
hi heng03....
Invoke setFocus() after setVisible(). And add a line that disable request focus of the default component that has the focus by using setRequestFocusEnabled(fal se). By default, the requestFocus is enabled.
See code below...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
JTextField field1, field2, field3, field4;
public Test(String title) {
super(title);
setLocation(300,220);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel myPanel = new JPanel();
field1 = new JTextField("field1");
field2 = new JTextField("field2");
field3 = new JTextField("field3");
field4 = new JTextField("field4");
myPanel.add(field1);
myPanel.add(field2);
myPanel.add(field3);
myPanel.add(field4);
getContentPane().add(myPan el);
pack();
setVisible(true);
field1.setRequestFocusEnab led(false) ; //disable field1 from requesting focus
field3.requestFocus();
}
public static void main(String[] args) {
Test myframe = new Test("Test");
}
}
Hope this helped...
Jerson
Invoke setFocus() after setVisible(). And add a line that disable request focus of the default component that has the focus by using setRequestFocusEnabled(fal
See code below...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
JTextField field1, field2, field3, field4;
public Test(String title) {
super(title);
setLocation(300,220);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel myPanel = new JPanel();
field1 = new JTextField("field1");
field2 = new JTextField("field2");
field3 = new JTextField("field3");
field4 = new JTextField("field4");
myPanel.add(field1);
myPanel.add(field2);
myPanel.add(field3);
myPanel.add(field4);
getContentPane().add(myPan
pack();
setVisible(true);
field1.setRequestFocusEnab
field3.requestFocus();
}
public static void main(String[] args) {
Test myframe = new Test("Test");
}
}
Hope this helped...
Jerson
hi heng03....
Invoke setFocus() after setVisible(). And add a line that disable request focus of the default component that has the focus by using setRequestFocusEnabled(fal se). By default, the requestFocus is enabled.
See code below...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
JTextField field1, field2, field3, field4;
public Test(String title) {
super(title);
setLocation(300,220);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel myPanel = new JPanel();
field1 = new JTextField("field1");
field2 = new JTextField("field2");
field3 = new JTextField("field3");
field4 = new JTextField("field4");
myPanel.add(field1);
myPanel.add(field2);
myPanel.add(field3);
myPanel.add(field4);
getContentPane().add(myPan el);
pack();
setVisible(true);
field1.setRequestFocusEnab led(false) ; //disable field1 from requesting focus
field3.requestFocus();
}
public static void main(String[] args) {
Test myframe = new Test("Test");
}
}
Hope this helped...
Jerson
Invoke setFocus() after setVisible(). And add a line that disable request focus of the default component that has the focus by using setRequestFocusEnabled(fal
See code below...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
JTextField field1, field2, field3, field4;
public Test(String title) {
super(title);
setLocation(300,220);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel myPanel = new JPanel();
field1 = new JTextField("field1");
field2 = new JTextField("field2");
field3 = new JTextField("field3");
field4 = new JTextField("field4");
myPanel.add(field1);
myPanel.add(field2);
myPanel.add(field3);
myPanel.add(field4);
getContentPane().add(myPan
pack();
setVisible(true);
field1.setRequestFocusEnab
field3.requestFocus();
}
public static void main(String[] args) {
Test myframe = new Test("Test");
}
}
Hope this helped...
Jerson
Sorry I accidentally refreshed the browser... :)
ASKER
Hi jerch, pls answer to this question
instead of comment.
I'll grant you the points.
Thanks
instead of comment.
I'll grant you the points.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
heng03
heng03
JComponent myComp = new JComponent();
myComp.setRequestFocusEnab
//then you request the focus:
myComp.requestFocus();
That should do it!
Laminamia