简体   繁体   English

JTextPane将其父Scrollpane滚动到底部

[英]JTextPane is scrolling its parent Scrollpane to the bottom

Here is a littel SSCCE which shows the weird scrolling behaviour. 这是一个小小的SSCCE,它显示了奇怪的滚动行为。 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. 到目前为止,我发现,这只发生在JTextPanes上,甚至没有JTextArea。 It also only happens if you are on the EDT. 只有你在美国东部时间才会发生这种情况。 Remove the invokeLater() from the SSCCE and it works as expected. 从SSCCE中删除invokeLater(),它按预期工作。 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);

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. 因此,我想知道实际触发滚动的内容,因此我可以扩展JTextPane或它使用的StyledDocument或其他任何内容,以避免默认情况下。 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. 在我的情况下,我主要使用JTextPane作为不可编辑的多行文本,因为它支持对齐文本和不同的字体大小和样式。 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. 如果可能的话,我不想在添加完所有内容后设置ScrollPane的滚动位置,因为我发现这是一个非常糟糕的解决方法。

Thanks for your help. 谢谢你的帮助。 Here is the SSCCE: 这是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);

            }
        });

    }

}

weird thingy: setting the update policy of the textPanes does make a difference - if done before setting the text 奇怪的是:设置textPanes的更新策略确实有所作为 - 如果在设置文本之前完成

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

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 .. ) 奇怪的是它改变了层次结构中更高的scrollPane的滚动行为(窗格被添加到一个面板,然后被包装到scrollPane中......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM