英文:
How do I enable a button after the scrollbar goes to the bottom?
问题
所以,我使用了一个 JScrollPane
,然后我添加了一个 JTextArea
。我使用了 textArea.setCaretPosition(0)
来重置滚动条,它就会回到顶部。一切正常,直到我想在滚动条到达底部时将一个禁用的按钮设置为可用。
我该如何做到这一点?
英文:
So, I used a JScrollPane
and then I added a JTextArea
. I used textArea.setCaretPosition(0)
to reset the scroll and it went at the top. All good, until I wanted to set a disabled Button on enable when the scrollbar reaches at the bottom.
How can I do that?
答案1
得分: 0
你可以监听 JScrollPane 的视口变化,并将视口可见矩形的底部与视口视图的高度(即 JTextArea)进行比较:
JViewport viewport = scrollPane.getViewport();
viewport.addChangeListener(e -> {
Rectangle rect = viewport.getViewRect();
int bottom = rect.y + rect.height;
endButton.setEnabled(bottom >= viewport.getViewSize().height);
});
英文:
You can listen for changes to the JScrollPane’s viewport, and compare the bottom of the viewport’s visible rectangle with the height of the viewport’s view (that is, the JTextArea):
JViewport viewport = scrollPane.getViewport();
viewport.addChangeListener(e -> {
Rectangle rect = viewport.getViewRect();
int bottom = rect.y + rect.height;
endButton.setEnabled(bottom >= viewport.getViewSize().height);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论