Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 626065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:22:41+00:00 2026-05-13T19:22:41+00:00

The problem: I am trying to debug some focus-related problems in my Java Swing

  • 0

The problem:

I am trying to debug some focus-related problems in my Java Swing application. There are times when some components seem to be grabbing focus and I cannot figure out where in code this is happening.

What I have tried:

  • A VetoableChangeListener with KeyboardFocusManager (for focusOwner). This does give me information about which components lose and gain focus, but it does not help me pin point where in code the focus is being requested.

  • A custom KeyboardFocusManager. But in that too I can intervene only when it receives events. By that time the call stack of the call to requestFocus is already lost.

  • A custom EventQueue. But there too I am able to intervene in the dispatchEvent method which is again called from the EDT. Again the call stack is lost (interestingly the postEvent(AWTEvent) is not called).

Question:

What I am looking for is the call stack when the call to requestFocusInWindow is made. Is it possible to get this information. Perhaps, if I could redefine the method used to post the event in EventQueue, then I can print the stack dump. However the EventQueue.postEvent(AWTEvent) does not get invoked.

Can anyone suggest a solution which will help me get the state of the stack when the call to requestFocus or requestFocusInWIndow may have been made?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:22:41+00:00Added an answer on May 13, 2026 at 7:22 pm

    It seems they (Sun) really don’t want you to do that. At first glance there don’t appear to be any virtual methods in that path that can easily be overridden, not in EventQueue (postEvent is used only for invokeLater and synthesising events from application code) nor in KeyboardFocusManager (as you’ve discovered, the overridable methods are called later from the dispatch loop.)

    Fortunately, if you are using the Sun JRE, there is a place you can insert code, but it’s not pretty:

    Component.requestFocus() calls the static KeyboardFocusManager.setMostRecentFocusOwner(Component), which updates a private static Map called mostRecentFocusOwners.

    So if you can access that static Map using reflection, you can replace it with a forwarding Map that traces calls to its put method:

    import com.google.common.collect.ForwardingMap;
    
    // ...
    
    Field mrfoField = KeyboardFocusManager.class.getDeclaredField("mostRecentFocusOwners");
    mrfoField.setAccessible(true);
    final Map delegate = (Map) mrfoField.get(null);
    Map mrfo = new ForwardingMap() {
        public Object put(Object key, Object value) {
            new Throwable().printStackTrace();
            return super.put(key, value);
        }
        protected Map delegate() {
            return delegate;
        }
    };
    mrfoField.set(null, mrfo);
    

    And this will catch calls to requestFocus and give you the stack traces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try like this: var mock = MockRepository.GenerateMock<Service>( new object[] {… May 14, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer Regarding imputation of categorical data, I would suggest to check… May 14, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer Add to vimrc: :let manpageview_multimanpage=0 pgup-pgdn mapped to changing sections… May 14, 2026 at 10:09 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.