Solved

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

Posted on 2000-03-11
8
1,243 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 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

23 Experts available now in Live!

Get 1:1 Help Now