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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:02:22+00:00 2026-06-12T02:02:22+00:00

I have some code which tries to determine what text is within a given

  • 0

I have some code which tries to determine what text is within a given vertical slice of a text area where the vertical slice is specified as Y coordinates instead of lines.

Converting to using line maths is a fine workaround for this issue, by the way, so that is what I’m going to go with, but I can envisage cases where you might only have a Y coordinate and it seems like this sort of thing would come up so I’m going to ask it anyway.

I reduced my issue down to a fairly minimalist (lol Java) example. We display a frame with some text and then attempt to determine the character offset of the text closest to the start of the text area. We know from commonsense that it will be 0, but figuring that out programmatically is the problem.

public static void main(String[] args) throws Exception {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new RtlTest().run();
        }
    });
}

JFrame frame;
JTextArea textArea;

public void run() {
    textArea = new JTextArea(
        "\u05D4\u05D5\u05D3\u05E2\u05EA \u05D8\u05D9\u05D9\u05D2\u05E8 " +
        "\u05D8\u05E7\u05E1\u05D8 \u05D1\u05E2\u05D1\u05E8\u05D9\u05EA");

    frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            measure();
        }
    });
}

public void measure() {
    try {
        System.out.println("Either the line is left to right or it's right to left " +
                           "(or a mix), so one of these two values should be 0:");
        System.out.println(textArea.viewToModel(new Point(0, 0)));
        System.out.println(textArea.viewToModel(new Point(textArea.getWidth() - 1, 0)));

        Rectangle firstLetterView = textArea.modelToView(0);
        System.out.println("This one should definitely be 0, right? " +
                           "I mean, we got the coordinates from Swing itself:");
        System.out.println(textArea.viewToModel(new Point(firstLetterView.x,
                                                          firstLetterView.y)));

        frame.dispose();
    } catch (BadLocationException e) {
        throw new IllegalStateException(e);
    }
}

The output is rather surprising:

Either the line is left to right or it's right to left (or a mix),
    so one of these two values should be 0:
23
23
This one should definitely be 0, right? I mean, we got the coordinates from Swing itself:
24

Points of surprise:

  1. The character closest to the top left corner is not character 0.
  2. The character closest to the top right corner is not character 0.
  3. The character closest to the position of character 0 is not character 0.
  • 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-12T02:02:24+00:00Added an answer on June 12, 2026 at 2:02 am

    Add the following line in your code:

    textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    

    Your output numbers will change – but your text will show from the right side of the text area. I hope this is what you want.

    EDIT:

    According to this Hebrew and Arabic should be in RT orientation.

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

Sidebar

Related Questions

I have some code which takes strings representing hexadecimal numbers - hex colors, actually
I have some code which I want to document with in-body comments like so:
I have some code which I'm currently using to change the static-IP of a
I have some code which populates a hashtable with a question as the key
I have some code which handles data files and reports an error when it
We have some code which sorts a list of addresses based on the distance
I have some code which is built both on Windows and Linux. Linux at
I have some code which builds an array of date ranges. I then call
I have some code which I think has extra release statements. Is the code
I have some code which is been running by a backgroundworker I'd like some

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.