Java-设置jScrollBar的位置 [英] Java - Setting position of jScrollBar
问题描述
我在JScrollPane中有一个JTable,该表偶尔会获取新的数据行.最终,数据行的数量超过了一次显示的数量,因此ScrollPane启动了.我希望每次添加新数据时,滚动窗格"都跳到底部(达到最大值),所以我这样写,即在添加新行后立即调用:
I have a JTable in a JScrollPane, and the table gets new rows of data every once in a while. Eventually there are more rows of data than can be displayed at once and so the ScrollPane kicks in. I want the Scroll Pane to jump to the bottom (to its maximum value) every time new data is added, so I wrote this, which is called right after the new row is added:
jScrollPane1.getVerticalScrollBar().setValue(jScrollPane1.getVerticalScrollBar().getMaximum());
它工作得很好,但是有一个问题-它没有滚动到完整的底部.它始终会遗漏表格的一行,您需要手动向下滚动才能到达整个底部.
It works quite well, but there is a problem- It doesn't scroll to the complete bottom. It always leaves out one row of the table that you need to manually scroll down to reach the complete bottom.
我怀疑只有在方法结束后才以某种方式添加新的数据行(按下JButton并激活actionPerformed方法后将其激活).
My suspicion is that the new row of data is somehow added only after the method is over (it is activated after pressing a JButton and activating an actionPerformed kind of method).
该如何解决?
推荐答案
您需要使用SwingUtilities.invokeLater在当前处理事件结束时将其排队. 看起来像这样:
you need to queue it at the end of current processing events using SwingUtilities.invokeLater; it would look like this:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jScrollPane1.getVerticalScrollBar().setValue(jScrollPane1.getVerticalScrollBar().getMaximum());
}
}
);
否则将在当前事件期间运行,并且将在任何实际更新之前发生.
otherwise will run during the current event and will happen before any actual update.
记住将它们设置为final,以将其传递给匿名内部类
remember to make them variable final to pass them into an anonymous inner class
这篇关于Java-设置jScrollBar的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!