Link to home
Start Free TrialLog in
Avatar of heng03
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).
 
Avatar of Laminamia063099
Laminamia063099

First, when you create the component, you need to set it's requestFocusEnabled:
  JComponent myComp = new JComponent();
  myComp.setRequestFocusEnabled(true);
 
//then you request the focus:
  myComp.requestFocus();

That should do it!

Laminamia
Avatar of heng03

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
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(false). 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(myPanel);
        pack();
        setVisible(true);
        field1.setRequestFocusEnabled(false); //disable field1 from requesting focus
        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(false). 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(myPanel);
        pack();
        setVisible(true);
        field1.setRequestFocusEnabled(false); //disable field1 from requesting focus
        field3.requestFocus();
    }

    public static void main(String[] args) {
        Test myframe = new Test("Test");
    }
}

Hope this helped...

Jerson
Sorry I accidentally refreshed the browser... :)
Avatar of heng03

ASKER

Hi jerch, pls answer to this question
instead of comment.
I'll grant you the points.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of jerch
jerch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heng03

ASKER

Thanks

heng03