JScrollPane中的JTextArea,视图坐标平移

发布于 2024-09-16 11:08:57 字数 1831 浏览 5 评论 0原文

我正在尝试在视图和视口坐标之间进行转换。 但 JViewport/JScrollpane 似乎并不像记录的那样工作。 JViewport.toViewCoordinates() 认为视图始终位于组件的左上角,尽管情况显然并非如此。

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]

上面是人为的例子。我真正的 JTextArea 大于一行。我不需要 JTextArea“模型”坐标(文本中的偏移量)。我需要真正的二维坐标。

视图位置不应该是 (0,0),因为视口中的第一个可见字符实际上位于 JTextArea 的第三行。

关于使用 JScrollPane 时如何在视图和组件坐标之间进行转换还有其他建议吗?

--- 补充 ---

这也失败了。

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

--- 添加 ---

这是基于我收到的答案的最终工作版本。 它显示了 java.awt.Point[x=0,y=32] 这是我所期望的。

@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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2024-09-23 11:08:57

问题是获取 viewPosition() 的方法在视口实际滚动之前执行。这是因为有时 Swing 将代码添加到事件线程的末尾以供以后处理。

通常可以通过将代码包装在 SwingUtilities.invokeLater() 中来解决此问题,以便在 Swing 完成所有处理后执行代码。然而,在下面的简单演示中,我发现我需要添加两个 invokeLater() 方法。我不知道为什么。

向上/向下移动插入符号,您将看到视图位置发生变化。第二个值将包含正确的位置:

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();
            }
        });
    }
}

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();
            }
        });
    }
}
所有深爱都是秘密 2024-09-23 11:08:57

SwingUtilities.convertPoint

什么用吗?

could

SwingUtilities.convertPoint

be of use?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文