Solved

requestFocus() method for JTextField

Posted on 2000-05-04
8
627 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
  • 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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 …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

743 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

10 Experts available now in Live!

Get 1:1 Help Now