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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:51:54+00:00 2026-05-15T15:51:54+00:00

I am writing a text editor(in java) where I am using a JTextArea for

  • 0

I am writing a text editor(in java) where I am using a JTextArea for the main text editing and I am putting the text into a stringbuilder.

When I highlight the text and do something like delete that block of text, I want it to update in the stringbuilder.

So my question is, is there a way to get the first and last index of the highlighted text in the stringbuilder?

Thanks.

  • 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-15T15:51:55+00:00Added an answer on May 15, 2026 at 3:51 pm

    You can keep them in sync using a DocumentListener as below. However, I heavily recommend rethinking whatever approach you’re trying to take here. It seems like you’re trying to use an external StringBuilder as the “model” to your JTextArea, but the Document is the model. So I fail to see when this would be a good idea.

    public static void main(String[] args) {
        JTextArea area = new JTextArea();
        final StringBuilder builder = new StringBuilder();
        area.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                builder.delete(e.getOffset(), e.getOffset() + e.getLength());
                System.out.println("Removed " + e.getLength() + " chars from SB");
            }
    
            @Override
            public void insertUpdate(DocumentEvent e) {
                try {
                    builder.insert(e.getOffset(), 
                            e.getDocument().getText(e.getOffset(), e.getLength()));
                    System.out.println("Inserted " + e.getLength() + " chars into SB.");
                } catch ( BadLocationException ble ) {
                    ble.printStackTrace();
                }
            }
    
            @Override public void changedUpdate(DocumentEvent e) { /* no-op */ }
        });
    
        final JFrame frame = new JFrame("DocumentListener Test");
        frame.add(area);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("Final SB contents:");
                System.out.println(builder.toString());
            }
        });
        frame.pack(); frame.setVisible(true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a syntax highlighting text editor in Java and I've run into a
I am writing a text editor and had planned on using the richtextbox control
I'm writing an very simple text editor with highlight of digitis, in some keywords
I'm writing a text editor using C#, I know i'm just reinventing the wheel
i am writing a text editor in gtk 3 using python. in gtk 2
I am writing a text editor in C using gtk+-2.0 & gtksourceview-2.0. I'm trying
I am writing a text editor using gtk+-2.0 & gtksourceview-2.0. I am having trouble
I am writing a Text Editor using the Qt C++ framework. I'm using a
So, I'm writing a text editor, using MDI. I have a frame window, child
I am writing text editor in c# winforms and I want it to Highlight

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.