当滚动条滑动到底部时,我该如何启用一个按钮?

huangapple go评论81阅读模式
英文:

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);
});

huangapple
  • 本文由 发表于 2020年10月7日 05:02:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64233747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定