Solved

How to set Focus for Jcomponent such as JTextField, JRadioButton etc

Posted on 2000-03-11
8
1,244 Views
Last Modified: 2013-11-23
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).
 
0
Comment
Question by:heng03
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2607666
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
0
 

Author Comment

by:heng03
ID: 2607746
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
0
 
LVL 4

Expert Comment

by:jerch
ID: 2607878
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
0
 
LVL 4

Expert Comment

by:jerch
ID: 2607882
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:jerch
ID: 2607897
Sorry I accidentally refreshed the browser... :)
0
 

Author Comment

by:heng03
ID: 2611473
Hi jerch, pls answer to this question
instead of comment.
I'll grant you the points.

Thanks
0
 
LVL 4

Accepted Solution

by:
jerch earned 20 total points
ID: 2611492
thank you :)
0
 

Author Comment

by:heng03
ID: 2614371
Thanks

heng03
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now