Solved

requestFocus() method for JTextField

Posted on 2000-05-04
8
637 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

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
topping2 challenge 13 91
factorial example 4 40
servlet filter example 37 63
Which non-HTML GUI front end to use with Java? 3 22
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

777 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