JTextPane将其父级Scrollpane滚动到底部 [英] JTextPane is scrolling its parent Scrollpane to the bottom

查看:134
本文介绍了JTextPane将其父级Scrollpane滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个小SSCCE,显示了奇怪的滚动行为.启动它时,它会向下滚动到滚动窗格的底部.但我希望它保持领先地位.到目前为止,我发现这仅发生在JTextPanes上,甚至没有发生在JTextArea上.仅当您使用EDT时,也会发生这种情况.从SSCCE中删除invokeLater(),它会按预期工作.但是,(对我而言)这不是解决方案.

Here is a littel SSCCE which shows the weird scrolling behaviour. When you start it, it scrolls down to the bottom of the scrollpane. But I want it to stay on top. So far I found out, this only happens with JTextPanes and not even with JTextArea. It also only happens if you are on the EDT. Remove the invokeLater() from the SSCCE and it works as expected. However, that's not a solution (for me).

我也尝试过,但是没有效果:

I also tried that, but with no effect:

final DefaultCaret caret = (DefaultCaret) tp.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

我想要的是一个干净而通用的解决方案.因此,我想知道实际触发滚动的原因,因此我可以扩展JTextPane或它使用的StyledDocument或其他任何方式来默认避免这种情况.就我而言,我主要将JTextPane用于不可编辑的多行文本,因为它支持文本对齐以及不同的字体大小和样式.因此,如果我解决了滚动问题,那么实际上我可以放弃编辑功能.如果可能的话,我不想在添加所有内容后设置ScrollPane的滚动位置,因为我发现这是一个非常糟糕的解决方法.

What I want is a clean and generic solution. Therefore I would like to know what actually triggers the scrolling, so I can either extend JTextPane or the StyledDocument it uses or anything else to avoid that by default. In my case I mostly use the JTextPane for non-editable, multiline text because it supports aligning of text and different font sizes and styles. So actually I could waive the editing features, if I get this scrolling issue solved instead. If possible I DON'T want to set the scroll position of the ScrollPane after everything has been added, because I find this a quite bad workaround.

感谢您的帮助.这是SSCCE:

Thanks for your help. Here is the SSCCE:

    import java.awt.LayoutManager;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class ScrollPaneWithTextPanes
{
    public static void main(final String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                final JPanel p = new JPanel();
                final LayoutManager layout = new BoxLayout(p, BoxLayout.Y_AXIS);
                p.setLayout(layout);

                for (int i = 0; i < 10; i++)
                {
                    final JTextPane tp = new JTextPane();
                    tp.setText("This is some text in text pane " + i);
                    p.add(tp);

                    //                    final DefaultCaret caret = (DefaultCaret) tp.getCaret();
                    //                    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
                }

                final JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.getContentPane().add(new JScrollPane(p));
                f.setSize(800, 200);
                f.setLocation(0, 0);

                f.setVisible(true);

            }
        });

    }

}

推荐答案

奇怪的事情:设置textPanes的更新策略确实-如果在设置文本之前完成

weird thingy: setting the update policy of the textPanes does make a difference - if done before setting the text

for (int i = 0; i < 10; i++) {
    final JTextPane tp = new JTextPane();
    final DefaultCaret caret = (DefaultCaret) tp.getCaret();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    tp.setText("This is some text in text pane " + i);
    p.add(tp);

    // adding some other components simply leaves the scrollPane at the top
    // JComponent b = new JButton("This is some text in button "
    // + i);
    // p.add(b);
}

final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(p));

奇怪的是,它会更改层次结构中更高层次的scrollPane的滚动行为(将窗格添加到面板中,然后将其包装到scrollPane中..)

The weirdness is that it changes the scrolling behaviour of a scrollPane higher up in the hierarchy (the panes are added to a panel which then is wrapped into a scrollPane .. )

这篇关于JTextPane将其父级Scrollpane滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆