Initial focused object in container?

Posted on 2005-04-19
Last Modified: 2010-03-31

I have the following code (scene and bankContainer are both Container subclasses)

        bankContainer = new BankContainer();

inside the BankContainer constructor:

        userID = new BankSingleLineEntry(0,0,200,30,16); // subclass of Component
        userID.requestFocus(); // I want it to start focussed

However, that component wasn't starting with the focus. Using a debugger I found out it was because the requestFocus() call was failing because the bankContainer had no peer - the bankContainer is added to the scene *after* the BankContainer() constructor and requestFocus are called.

What's the right way to go about setting the initally focused object in a container?
Question by:eeBlueShadow
    LVL 15

    Expert Comment

    I guess this thread describes your problem:
    You can always solve it by calling requestFocus from the paintComponent method of BankContainer (and use a member variable
    flag isInitialized to make it happen only once).
    LVL 16

    Accepted Solution

    Try to call the requestFocus() after the bankContainer is added.
    Can you provide a method for call userID.requestFocus() inside BankContainer class?
    LVL 10

    Author Comment

    That worked perfectly.

    I solved the problem by overriding the Container.requestFocus() method to simply call Component.requestFocus()

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Automated mobile software testing 8 106
    countHi2 challenge 7 30
    mapShare challenge 13 40
    where is session ID cache stored 1 25
    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…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    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:
    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…

    758 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

    7 Experts available now in Live!

    Get 1:1 Help Now