问题
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);
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.
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);
}
});
}
}
回答1:
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));
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 .. )
来源:https://stackoverflow.com/questions/14277149/jtextpane-is-scrolling-its-parent-scrollpane-to-the-bottom