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

  • SEARCH
  • Home
  • 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 1033205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:11:49+00:00 2026-05-16T14:11:49+00:00

I’m trying to translate between view and viewport coordinates. But the JViewport/JScrollpane doesn’t seem

  • 0

I’m trying to translate between view and viewport coordinates.
But the JViewport/JScrollpane doesn’t seem to work as documented. JViewport.toViewCoordinates()
thinks the view is always at the top left of the component, even though that’s clearly not the case.

String text = "blahblahblah\nblahblah\nblah";
JFrame frame = new JFrame("title");
JTextArea textArea = new JTextArea(text, 1, 30); // shows only one line
frame.add(new JScrollPane(textArea));
frame.pack();
frame.setVisible(true);
textArea.setCaretPosition(text.length()); // now showing the last line
JViewport viewport = ((JViewport)textArea.getParent());

viewport.getViewRect(); // returns java.awt.Rectangle[x=0,y=0,width=330,height=16]

viewport.getViewPosition(); // returns java.awt.Point[x=0,y=0]

viewport.toViewCoordinates(new Point(0,0)); // returns java.awt.Point[x=0,y=0]

The above is contrived example. My real JTextArea is larger than one line. I don’t need JTextArea “model” coordinate (the offset in the text). I need genuine 2d coordinates.

The view position shouldn’t be (0,0), as the first visible character in the viewport is actually in the 3rd line of the JTextArea.

Any other suggestions on how I can translate between view and component coordinates when using JScrollPane?

— added —

This also fails.

SwingUtilities.convertPoint(viewport,0,0, textArea);
(java.awt.Point) java.awt.Point[x=0,y=0]

— added —

Here is the final working version, based on the answer I received.
it shows java.awt.Point[x=0,y=32] which is what I expected.

@Test
public void test() throws InterruptedException {

    String text = "blahblahblah\nblahblah\nblah";
    JFrame frame = new JFrame("title");
    JTextArea textArea = new JTextArea(text, 1, 30);
    frame.add(new JScrollPane(textArea));
    frame.pack();
    frame.setVisible(true);
    textArea.setCaretPosition(text.length());
    final JViewport viewport = ((JViewport)textArea.getParent());

    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run() {
            System.out.println(viewport.getViewPosition());
        }
    });

    Thread.sleep(1000);
}
  • 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-16T14:11:50+00:00Added an answer on May 16, 2026 at 2:11 pm

    The problem is that the method to get the viewPosition() executes before the viewport has actually been scrolled. This is because sometimes Swing adds code to the end of the event thread for later processing.

    Usually this problem can be solved by wrapping your code in a SwingUtilities.invokeLater() so the code is executed after Swing has done all its processing. However in the simple demo below I found I needed to add two invokeLater() methods. I’m not sure why.

    Move the caret up/down and you will see the view position change. The second value will contain the correct position:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class Test5
    {
        public static void createAndShowGUI()
        {
            String text = "one\ntwo\nthree\nfour\nfive";
            JFrame frame = new JFrame("title");
            JTextArea textArea = new JTextArea(text, 1, 30); // shows only one line
            JScrollPane scrollPane = new JScrollPane( textArea );
            frame.add(scrollPane);
            frame.pack();
            frame.setVisible(true);
    
            final JViewport viewport = scrollPane.getViewport();
    
            textArea.addCaretListener( new CaretListener()
            {
                public void caretUpdate(CaretEvent e)
                {
                    SwingUtilities.invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            System.out.println("First : " + viewport.getViewPosition() );
    
                            SwingUtilities.invokeLater(new Runnable()
                            {
                                public void run()
                                {
                                    System.out.println("Second: " + viewport.getViewPosition() );
                                }
                            });
                        }
                    });
                }
            });
    
            textArea.setCaretPosition(text.length());
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.