Solved

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

Posted on 2000-03-11
8
1,250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 160
hibernate example issues from command prompt 10 85
Is there a simpler dropbox system? 10 53
JVM error from eclipse 1 26
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

739 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