如何让JScrollPane添加组件后动态滚动到最后?
这是我尝试执行的 ScrollPane 代码
public class CustomScrollPane extends JScrollPane {
private static CustomScrollPane instance = null;
public CustomScrollPane () {
super(panel.getInstance()); // a panel that the scrollpane wraps around
this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// hide the vertical scroll bar
this.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
}
public static CustomScrollPane getInstance() {
if (instance == null)
instance = new CustomScrollPane ();
return instance;
}
getVerticalScrollbar.setValue(getVerticalScrollbar().getMaximum())
但当我向其中添加 JLabels 时,它不会滚动到末尾。我正在尝试使其在新的 JLabel 添加到面板后始终滚动到屏幕底部。我没有使用 JTextArea,因为我希望每行都有不同的前景色,所以我使用了 JLabels。
我也尝试添加此方法
public void scrollToBottom() { getVerticalScrollbar().getMaximum(); }
,但它只是冻结 ScrollPane 并且我无法滚动。
任何帮助将不胜感激!
Here is my code for the ScrollPane
public class CustomScrollPane extends JScrollPane {
private static CustomScrollPane instance = null;
public CustomScrollPane () {
super(panel.getInstance()); // a panel that the scrollpane wraps around
this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// hide the vertical scroll bar
this.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
}
public static CustomScrollPane getInstance() {
if (instance == null)
instance = new CustomScrollPane ();
return instance;
}
I tried to do getVerticalScrollbar.setValue(getVerticalScrollbar().getMaximum())
but it does not scroll to the end when I add JLabels to it. I'm trying to make it so that it will always scroll to the bottom of the screen once a new JLabel gets added to the panel. I din't use JTextArea as I want each line to have a different foreground color, so I used JLabels.
I also tried adding this method
public void scrollToBottom() { getVerticalScrollbar().getMaximum(); }
but it just freezes the ScrollPane and I am unable to scroll.
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你希望通过扩展
JScrollPane
实现什么,不确定这是否真的是它的核心职责,毕竟,你可以让它的“视口视图”自己完成,例如......魔法就在这里...
在这里,我要求
contentPane
(它是容纳所有方块的容器)基于以下内容“滚动到可见的矩形”我的需求现在,在您的情况下,当您将新组件添加到“容器”时,您需要指示组件滚动到显示新组件的位置。
这会有点复杂,因为您需要首先触发布局过程,这样您就可以获得容器的新大小
动态添加到容器中...
现在,如果您真的想解耦这个概念,您可以我们可以使用
ContainerListener
和对新组件做出反应通过它添加I'm not sure what you hope to achieve by extending
JScrollPane
, not sure that it's really its core responsibility, after all, you could have its "viewport view" do it itself, for example...The magic is right here...
Here, I'm asking the
contentPane
(which is the container holding all the squares) to "scroll to a visible rectangle" based on my needsNow, in you case, when you add a new component to the "container", you would need to instruct the component to scroll to a position which would show the new component.
This is going to be a little more complicated as you will need to trigger a layout pass first, so you can get the new size of the container
Dynamic addition to the container...
Now, if you really wanted to decouple the concept, you could us a
ContainerListener
and react to the new component been added via it