Solved

requestFocus() method for JTextField

Posted on 2000-05-04
8
654 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

631 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