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 4537168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:38:15+00:00 2026-05-21T14:38:15+00:00

Suppose I have a Java application that has more than one component in which

  • 0

Suppose I have a Java application that has more than one component in which you can enter text. Now suppose this application also has a dialog that lets you insert a single character (like the dialog in Word that comes up when you select Insert from the Edit menu) into those components. You want it to insert the character into whichever text component last had the focus.

But how do you know which text component last had the focus?

I could keep track of this manually, by having each text component report to the application whenever it gets the focus and then have the application insert the new character into whichever component that last had the focus.

But this must be a common problem (consider Paste buttons in tool bars—how does it know where to paste it into?). Is there something already built in to Swing that lets you get a handle to the last text component that had the focus? Or do I need to write this myself?

  • 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-21T14:38:16+00:00Added an answer on May 21, 2026 at 2:38 pm

    Is there something already built in to Swing that lets you get a handle to the last text component that had the focus?

    You create an Action that extends TextAction. The TextAction class has a method that allows you to obtain the last text component that had focus.

    Edit:

    You can create your own Action and do whatever you want. The Action can then be added to any JMenuItem or JButton. For example:

    class SelectAll extends TextAction
    {
        public SelectAll()
        {
            super("Select All");
        }
    
        public void actionPerformed(ActionEvent e)
        {
            JTextComponent component = getFocusedComponent();
            component.selectAll();
        }
    }
    

    If you just want to insert a character at the caret position of the text field then you can probably just do

    component.replaceSelection(...);
    

    Edit 2:

    I don’t understand what the confusion is with this answer. Here is a simple example:

    1. select some text
    2. use the mouse to click on the check box
    3. tab or use the mouse to click on the “Cut” button

    It doesn’t matter that the text field doesn’t currently have focus when the Action is invoked. The TextAction tracks the last text component that had focus.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class TextActionTest extends JFrame
    {
        JTextField textField = new JTextField("Select Me");
        JTabbedPane tabbedPane;
    
        public TextActionTest()
        {
            add(textField, BorderLayout.NORTH);
            add(new JCheckBox("Click Me!"));
            add(new JButton(new CutAction()), BorderLayout.SOUTH);
        }
    
        public static void main(String[] args)
        {
            TextActionTest frame = new TextActionTest();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
         }
    
        class CutAction extends TextAction
        {
            public CutAction()
            {
                super("Click to Cut Text");
            }
    
            public void actionPerformed(ActionEvent e)
            {
                JTextComponent component = getFocusedComponent();
    //          JTextComponent component = getTextComponent(e);
                component.cut();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have a Java program within an IDE (Eclipse in this case).
Suppose that I have a Java class with a static method, like so: class
In Java, suppose I have a String variable S, and I want to search
I have a Serializable object which is supposed to hold a java.awt.Image as its
I am using java language,I have a method that is supposed to return an
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
Suppose I have a class module clsMyClass with an object as a member variable.

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.