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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:16:13+00:00 2026-05-21T05:16:13+00:00

I would like to know how can I extend JEditorPane (or any other swing

  • 0

I would like to know how can I extend JEditorPane (or any other swing text editing component) to handle rectangle (column) selection mode. It is a well known feature in current text editors where you can select multiple lines (rows) starting from an offset (column) and ending by an offset (column) which look like selecting a rectangle of text, and then what you type will overwrite the selection in each line (row) concurrently.

One idea was to override the selection and create fake selection by highlighting each line in rectangle form by following the mouse events, and keeping track of such information to use it when typing. However, I am not quit sure how to override the selection and track the mouse, nor how to redirect typing to affect each line.

Any help in any form would be appreciated.

  • 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-21T05:16:13+00:00Added an answer on May 21, 2026 at 5:16 am

    Found out this little code snippet, that involves a Custom Caret (to handle the fragmented selection) and Highlighter (to show the fragments):

    class MyCaret extends DefaultCaret {
    
    Point lastPoint=new Point(0,0);
    public void mouseMoved(MouseEvent e) {
        super.mouseMoved(e);
        lastPoint=new Point(e.getX(),e.getY());
    }
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        getComponent().getHighlighter().removeAllHighlights();
    }
    
    protected void moveCaret(MouseEvent e) {
        Point pt = new Point(e.getX(), e.getY());
        Position.Bias[] biasRet = new Position.Bias[1];
        int pos = getComponent().getUI().viewToModel(getComponent(), pt, biasRet);
        if(biasRet[0] == null)
            biasRet[0] = Position.Bias.Forward;
        if (pos >= 0) {
            setDot(pos);
            Point start=new Point(Math.min(lastPoint.x,pt.x),Math.min(lastPoint.y,pt.y));
            Point end=new Point(Math.max(lastPoint.x,pt.x),Math.max(lastPoint.y,pt.y));
            customHighlight(start,end);
        }
    }
    
    protected void customHighlight(Point start, Point end) {
        getComponent().getHighlighter().removeAllHighlights();
        int y=start.y;
        int firstX=start.x;
        int lastX=end.x;
    
        int pos1 = getComponent().getUI().viewToModel(getComponent(), new Point(firstX,y));
        int pos2 = getComponent().getUI().viewToModel(getComponent(), new Point(lastX,y));
        try {
            getComponent().getHighlighter().addHighlight(pos1,pos2,
                     ((DefaultHighlighter)getComponent().getHighlighter()).DefaultPainter);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        y++;
        while (y<end.y) {
            int pos1new = getComponent().getUI().viewToModel(getComponent(), new Point(firstX,y));
            int pos2new = getComponent().getUI().viewToModel(getComponent(), new Point(lastX,y));
            if (pos1!=pos1new)  {
                pos1=pos1new;
                pos2=pos2new;
                try {
                    getComponent().getHighlighter().addHighlight(pos1,pos2,
                             ((DefaultHighlighter)getComponent().getHighlighter()).DefaultPainter);
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            y++;
        }
    }
    }
    

    Anyway, I’ve never run that code (it’s Stanislav’s).

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

Sidebar

Related Questions

I would like to know if there are any tools that can help me
I would like to know where can I find the code which eclipse uses
I would like to know how Can I paas Page as Ref Parameter to
I would like to know if I can open 2 different diagrams using MS
I would like to know if I can install say Visual Studio 2008 Pro
I would like to know how I can refer to a list item object
I have a generator and I would like to know if I can use
I'm using SQL Server 2005 and would like to know how I can get
I would like to know which one is the best material that I can
OK I would like to know this once and for all, how can I

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.