?
Solved

Setting focus in a complex panel

Posted on 2003-02-22
4
Medium Priority
?
562 Views
Last Modified: 2009-07-29
I have a BorderLayout main frame whose Center is a GridLayout, 12 rows and 9 columns, and I've set most of the cells (JTextFields) to setEditable( false ).  I want to start with focus on a specific editable cell, but <cellname>.requestFocus() has no effect.  I can successfully shift focus between editable cells once I've established focus, but how do I get the initial focus?
0
Comment
Question by:Bob_Everard
[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
  • 2
4 Comments
 

Expert Comment

by:raffael
ID: 8002191
Perhaps you used requestFocus in an incorrect way.
requestFocus() just works if your window with the textfields is already visible.

Best regards, Raffael Vogler
0
 

Expert Comment

by:raffael
ID: 8002196
You could addionally give a try to:

SwingUtilities.invokeLater(new Runnable(){
  public void run(){
    [requestFocus()]
  }
});

Best regards, Raffael Vogler
0
 
LVL 9

Accepted Solution

by:
yongsing earned 150 total points
ID: 8002629
You can set focus on a component only when it's visible on screen. What you can do is to add a window listener to your JFrame. When the JFrame is opened (become visible), you set the focus to the cell.

frame.addWindowListener(
  new WindowAdapter() {
    public void windowOpened(WindowEvent e) {
      <cellname>.requestFocus();
    }
  }
);
0
 
LVL 2

Author Comment

by:Bob_Everard
ID: 8003605
Thanks, raffael, for your prompt answer.

I tried adding the following code round the requestFocus:

      int l = 0;
      while ( !scoreText[ 0 ].isVisible() ) {
         System.out.println( "Waiting(" + ++l +
                             ") for visibility for scoreText[ 0 ]..." );
         l++;
      } // while
      scoreText[ 0 ].requestFocus();
      l = 0;
      while ( !scoreText[ 0 ].hasFocus() ) {
         System.out.println( "Waiting(" + ++l +
                             ") for focus for scoreText[ 0 ]..." );
      } // while

The interesting thing is that sometimes I go through the "Waiting for focus" loop 3 times, sometimes 5, but often I have to force focus by clicking on the field to avoid an infinite wait.  And the whole purpose of this exercise is to avoid having to select focus by hand.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

770 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