java 在 JScrollPane 上设置滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1166072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Scroll Bar on a JScrollPane
提问by twolfe18
I have this JTextPane(wrapped in a JScrollPane) that is backed by a HTMLEditorKit. The contents of the JTextPaneis 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(包裹在 a 中JScrollPane)由HTMLEditorKit. 的内容JTextPane是简单的 HTML,其中使用 img 标签嵌入了一些图像(本地文件)。问题是,当您加载 时,加载JTextPane需要一瞬间,然后它会在页面底部出现滚动条。如果我做:
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:
它会暂时设置滚动条,然后另一个thead(大概是负责加载图像)出现并将滚动条敲回底部。我尝试添加:
((AbstractDocument)text.getDocument()).setAsynchronousLoadPriority(-1);
but that did not fix it. Is there any way to get an event from either text.getDocument()or textthat 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.
但这并没有解决它。有没有办法从任一得到一个事件text.getDocument()或text当窗格完成加载,这样我可以设置滚动条,然后会通知我吗?另一种方法是我设置另一个线程等待一秒钟左右,然后设置滚动条,但这是一个糟糕的技巧。
Your suggestions?
你的建议?
回答by Spyder
Have you tried using invokeLater?
您是否尝试过使用 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.
您还可以在 setValue() 方法中使用调试器,而不仅仅是打印堆栈跟踪。
回答by phil294
The following solved the problem for me, after 50 minutes of despair:
经过50分钟的绝望,以下为我解决了问题:
JTextPane.grabFocus();
JTextPane.setCaretPosition(20);
回答by Savvas Dalkitsis
Is your app single threaded by any chance? If it is you can request a list of running threads and get notified when they finish and then set the scrollbar value. Is that an option?
你的应用程序是单线程的吗?如果是,您可以请求正在运行的线程列表并在它们完成时得到通知,然后设置滚动条值。这是一个选择吗?
Or you can provide an ImageObserver to every image loaded and set the position of the scrollbar when all images are reported loaded?
或者您可以为每个加载的图像提供一个 ImageObserver 并在报告所有图像加载时设置滚动条的位置?

