Solved

requestFocus() method for JTextField

Posted on 2000-05-04
8
650 Views
Last Modified: 2012-05-04
Hi!
For my TextField component I use requestFocus() method and it works fine. The problem is when I change my component to JTextField. Now I dont manage to get focus using the described method. Why?
Thanks.
0
Comment
Question by:kalle73
[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 7

Expert Comment

by:Sasha_Mapa
ID: 2777173
Umm, you should be able actually. requestFocus() (as someone mentioned) works only when the component is already shown. So you must first add it to whatever container (which needs to be already on the screen) and only then requestFocus() will work.
0
 

Author Comment

by:kalle73
ID: 2777257
I use EXACTLY the same code. Then only change is TextField -> JTextField and thus the focus is lost.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2777518
post some small (compilable) example that reproduces the problem.
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

 

Author Comment

by:kalle73
ID: 2777760
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;

public class AdTaker {
  public AdTaker() {
    PasswordFrame passFrame =
      new PasswordFrame();
    passFrame.setLocation(250,200);
    passFrame.validate();
    passFrame.setVisible(true);
    passFrame.adTaker.setRequestFocusEnabled
      (true);
    passFrame.adTaker.requestFocus();  
  }
  public static void main(String[] args) {
    new AdTaker();
  }
}

import java.awt.*;
import java.util.*;
import javax.swing.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
import java.awt.event.*;

public class PasswordFrame extends Frame {
  JPanel panel = new JPanel();
  public TextField adTaker = new TextField();
  public PasswordFrame() {
    try  {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {
    panel.add(adTaker,
      new GridBagConstraints2(3, 0, 1, 2, 1.0, 0.0,
      GridBagConstraints.WEST,
      GridBagConstraints.HORIZONTAL,
      new Insets(27, 20, 0, 0), 127, 0));
    this.getContentPane().add(
      panel, BorderLayout.CENTER);
  }
}
0
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 50 total points
ID: 2780192
Unfortunately I compile your program because:
1. setRequestFocusEnabed is not defined in TextField.
2. GridBagConstraints2 is not defined in the JDK at all, I assume it's in borland.jbcl.layout.* which I don't have.
3. Frame does not define getContentPane().

Because of (2) I can't even fix your problem. (2) actually might be the reason of your problem. Try writing something smaller and simpler, like just adding a textfield and requesting the focus, and then adding the JTextField and requesting focus.
This problem shouldn't happen at all because requestFocus is done on the Component level, and not on the TextField or JTextField level... I think.
0
 

Author Comment

by:kalle73
ID: 2785423
OK, I solved it. Just moved _everything_ to swing and it worked. So it was on the TextField-JTextField level actually. But for your try - grab your points. And thanks.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2785436
Thanks for the points, but it still can't be on the TextField - JTextField level, ALL code related to focus is found in the Component (and I think some in the Container) class, none of it is overriden in TextField or JTextField.
0
 

Author Comment

by:kalle73
ID: 2787902
Maybe. But that's how the things went. When I changed Panel to JPanel, Frame to JFrame and so on, it began to work. No other changes...
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java: How do I open the default windows program for a given file type 3 63
jsp login check 12 67
How to convert from xls to xlsx using java 7 168
Java pass by reference 3 74
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:
Suggested Courses

752 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