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

  • Home
  • SEARCH
  • 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 8171595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:30:33+00:00 2026-06-06T21:30:33+00:00

I am working with a Swing program and having a little trouble. The program

  • 0

I am working with a Swing program and having a little trouble. The program has two windows (both are JFrames). The main window is just fine and should not be relevant to this issue.

The window I am having issues with contains a JScrollPane with a JPanel in it, and has a JMenuBar. The JPanel has a bunch of JTextComponents (some JTextFields, some JTextAreas) on it.

What I want to do is have an ActionListener attached to a JMenuItem find the JTextComponent that has focus.

I have seen the previous posts at focused component reference and How to find out which object currently has focus. My issue is that calling the particular window’s getFocusOwner() method merely returns the JFrame’s JRootPane, which is utterly unhelpful. Both the JScrollPane and the JPanel in question are focusable according to their isFocusable() methods. This happens even if I actually enter text into one of the JTextComponents before clicking the menu item. The cursor still blinks in the text field while I open the menu, and everything. For what it’s worth, getMostRecentFocusOwner() also simply returns the JRootPane.

  • 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-06-06T21:30:35+00:00Added an answer on June 6, 2026 at 9:30 pm

    I think I have solved this, because of the fact that you lose focus when you click on a menu item, we simply have to wait for focus to return to the component before we check who has focus, I have done this using a swing timer that waits 100ms and then executes its method to check which component has focus:

    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.TimerTask;
    import javax.swing.*;
    
    public class JavaApplication180 extends JFrame {
    
        private JTextField[] JTextFields;
        private JMenuBar menuBar;
        private JMenu menu;
        private JMenuItem item;
    
        public JavaApplication180() {
            initComponents();
    
            createAndShowUI();
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new JavaApplication180();
                }
            });
        }
    
        private void createAndShowUI() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().setLayout(new GridLayout(2, 2, 10, 10));
            setJMenuBar(menuBar);
            addComponentsToPane();
    
            pack();
            setVisible(true);
        }
    
        private void initComponents() {
            JTextFields = new JTextField[4];
    
            menuBar = new JMenuBar();
            item = new JMenuItem("Who has focus?");
            item.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    TimerTask tt = new TimerTask() {
    
                        @Override
                        public void run() {
                            JOptionPane.showMessageDialog(null, getMostRecentFocusOwner().getName());
                        }
                    };
                    new java.util.Timer().schedule(tt, 100);
                }
            });
    
            menu = new JMenu("File");
            menu.add(item);
            menuBar.add(menu);
        }
    
        private void addComponentsToPane() {
            for (int i = 0; i < JTextFields.length; i++) {
                JTextFields[i] = new JTextField();
                JTextFields[i].setText(String.valueOf(i));
                JTextFields[i].setName(String.valueOf(i));
                getContentPane().add(JTextFields[i]);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a console window in Swing. It's based on a JTextArea
I'm working on a simple calculator program using java and javax.swing Basically, when you
I'm having some trouble with an assignment I'm working on. Part of it is
I'm working on a simple little swing component, and I'm tearing out my hair
I'm just playing with Swing and I'm working on a really simple Swing component.
I'm working on a swing program to display several pictures. And one can rotate
I have been working on a Swing based java program for a while now,
So, I'm working with swing and I need to find a clean (non-CPU-hogging-way) to
I'm working on a swing app that will allow users to view sets of
I'm working on a Swing application (currently running on Java 1.6 update 11) which

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.