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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:05:27+00:00 2026-06-06T10:05:27+00:00

I am working on a simple java swing based application. How would I get

  • 0

I am working on a simple java swing based application. How would I get and set the text of the currently focused textfield/textarea of a form?

I know how to determine which component has focus but I can not figure out how I can get the selected text of the component. I use getFocusOwner() but it return a Component and therefore does not implement the method getSelectedText(). Do I somehow need to do a typecast?

  • 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-06T10:05:29+00:00Added an answer on June 6, 2026 at 10:05 am

    Depending on your exact context, you might consider to use a custom TextAction: its method getTextComponent(ActionEvent) returns the most recent focused text component. A code snippet:

        Action logSelected = new TextAction("log selected") {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                JTextComponent text = getTextComponent(e);
                System.out.println("selected: " + text.getSelectedText());
            }
    
        };
    
        JComponent content = new JPanel();
        content.add(new JTextField("sometext", 20));
        content.add(new JTextField("other content", 20));
        content.add(new JCheckBox("just some focusable comp"));
        content.add(new JButton(logSelected));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a simple desktop java application. I would like to make
I'm working on converting a very simple java desktop application to run in java
I am trying to convert a working simple Java application to an applet. The
I'm working on a java SE 1.5+ swing application, in conjunction with others. I'm
I'm working on a simple calculator program using java and javax.swing Basically, when you
I am currently working on a small desktop Java application which will track various
Im working on a very simple project in java that utilizes the Swing component
I am currently working on a simple GUI application that rolls two dice. I
I'm working on a pretty simple Java app in order to learn more about
I'm trying to make a simple text editor with winapi, its working for simple

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.