Solved

requestFocus() method for JTextField

Posted on 2000-05-04
8
633 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 91
unix in java example 9 53
Problem to start Neon 20 89
stackato and cloud 4 74
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
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:
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…

863 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

26 Experts available now in Live!

Get 1:1 Help Now