Solved

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

Posted on 2000-03-11
8
1,246 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse argument 14 61
Arrays.asList  VS  ArrayList 4 60
varialbe initialization 11 33
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 36
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

810 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