Swing - 确保文本组件中的可见行

时间:2011-06-07 07:15:01

标签: java swing jscrollpane viewport jeditorpane

我有一些文本组件(特别是它JEditorPane),并且需要作为对某些事件的响应以使文本组件中的某些行可见 - 即如果必要则滚动到它。如何用Swing做到这一点?

我找到了setCaretPosition,但并不总是好的。如果插入符号已经设置为新的位置,则不会再次显示它。

3 个答案:

答案 0 :(得分:3)

来自教程How to Use Editor Panes and Text PanesHow to Use Scroll Panes,您可以JViewPort确定可见的矩形

示例:

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class IsRectVisible {

    private static void createAndShowUI() {
        JFrame frame = new JFrame("IsRectVisible");
        frame.getContentPane().add(new IsRectVisibleGui());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }

    private IsRectVisible() {
    }
}

class IsRectVisibleGui extends JPanel {

    public static final Rectangle RECT = new Rectangle(450, 400, 100, 100);
    public static final Dimension INNER_PANEL_SIZE = new Dimension(600, 800);
    private static final Dimension SCROLLPANE_SIZE = new Dimension(250, 300);
    private static final String NOT_VISIBLE = "Not Visible";
    private static final String VISIBLE = "Visible";
    private static final long serialVersionUID = 1L;
    private InnerPanel innerPanel = new InnerPanel();
    private JViewport viewport = new JViewport();
    private JLabel statusLabel = new JLabel(NOT_VISIBLE);

    IsRectVisibleGui() {
        JScrollPane scrollpane = new JScrollPane();
        scrollpane.setViewport(viewport);
        viewport.add(innerPanel);
        scrollpane.setPreferredSize(SCROLLPANE_SIZE);
        viewport.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                Rectangle viewRect = viewport.getViewRect();
                if (viewRect.intersects(RECT)) {
                    statusLabel.setText(VISIBLE);
                } else {
                    statusLabel.setText(NOT_VISIBLE);
                }
            }
        });

        setLayout(new BorderLayout());
        add(scrollpane, BorderLayout.CENTER);
        add(statusLabel, BorderLayout.SOUTH);
    }

    class InnerPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        InnerPanel() {
            setPreferredSize(INNER_PANEL_SIZE);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.red);
            g2.setStroke(new BasicStroke(4));
            g2.draw(RECT);
        }
    }
}

您可以使用JScrollPane#scrollRectToVisible(Rectangle aRect)

移动它

答案 1 :(得分:2)

使用modelToView()方法传递偏移量并获取矩形。然后使用scrollRectToVisible

答案 2 :(得分:0)

像往常一样,StanislavL完全正确:)。 你必须保存你想去的地方。 比你得到你需要的ractengle

Rectangle ractYouNeed = thePane.getUI().modelToView(thePane, position);

之后,您可以轻松滚动到该矩形或其他任何内容。