问题描述
我有一个 JTextPane
(包裹在一个 JScrollPane
),由一个HTMLEditorKit
。 JTextPane
的内容是使用img标签嵌入的一些图像(本地文件)的简单HTML。问题是,当您加载 JTextPane
时,需要一秒钟才能加载,然后出现在页面底部的滚动条。如果我这样做:
JTextPane text = new JTextPane();
JScrollPane scroll = new JScrollPane(text);
//做一些设置...
scroll.getVerticalScrollBar()。setValue(0);
它立即设置滚动条,然后设置另一个(可能是负责加载图像)来滚动滚动条回到底部。我尝试添加:
((AbstractDocument)text.getDocument())。setAsynchronousLoadPriority(-1);
但是没有修复它。有没有办法从 text.getDocument()
或文本
中获取事件,当窗格会通知我完成加载,以便我可以设置滚动条呢?另一种选择是我设置另一个线程等待一秒钟,然后设置滚动条,但这是一个坏的黑客。
您的建议? p>
您是否尝试使用invokeLater?
final JScrollPane scroll = new JScrollPane(text);
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
scroll.getVerticalScrollBar()。setValue(0);
}
});
如果这不起作用,挖掘图像视图是非常困难的,所以我的下一步将是跟踪滚动条的变化原因:
scroll.setVerticalScrollbar(new JScrollBar(){
public void setValue int value){
new Exception()。printStackTrace();
super.setValue(value);
}
});
您还可以使用setValue()方法中的调试器,而不是仅打印堆栈跟踪。 / p>
I have this JTextPane
(wrapped in a JScrollPane
) that is backed by a HTMLEditorKit
. The contents of the JTextPane
is simple HTML with some images (local files) embedded using img tags. The problem is that when you load the the JTextPane
, it takes a split second to load and then it comes up with the scroll bar at the bottom of the page. If I do:
JTextPane text = new JTextPane();
JScrollPane scroll = new JScrollPane(text);
// do some set up...
scroll.getVerticalScrollBar().setValue(0);
it sets the scroll bar momentarily and then another thead (presumably that is in charge of loading the images) comes and knocks the scroll bar back to the bottom. I tried adding:
((AbstractDocument)text.getDocument()).setAsynchronousLoadPriority(-1);
but that did not fix it. Is there any way to get an event from either text.getDocument()
or text
that will notify me when the pane is finished loading so that I can set the scroll bar then? The alternative is that I set up another thread to wait a second or so, and then set the scroll bar, but this is a bad hack.
Your suggestions?
Have you tried using invokeLater?
final JScrollPane scroll = new JScrollPane(text);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
scroll.getVerticalScrollBar().setValue(0);
}
});
If that doesn't work, digging into the image views is pretty tough so my next step would be to track down why the scrollbar is changing:
scroll.setVerticalScrollbar(new JScrollBar() {
public void setValue(int value) {
new Exception().printStackTrace();
super.setValue(value);
}
});
You could also use a debugger in the setValue() method instead of just printing the stack trace.
这篇关于在JScrollPane上设置滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!