[英]How to make JScrollPane set to the bottom when a JButton is pressed?
I have a panel in which there is a JScrollPane
and a JButton
. 我有一个面板,其中有一个
JScrollPane
和一个JButton
。 I want to know how I can set the scroll pane's vertical scroll bar to bottom when I press the button. 我想知道当我按下按钮时如何将滚动窗格的垂直滚动条设置为底部。
I have tried code:- 我尝试过代码:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
writeClient();
jScrollPane2.getVerticalScrollBar().setValue(jScrollPane2.getVerticalScrollBar().getMaximum());
}
But it doesn't lead to extreme bottom - a little space is still left at the bottom. 但这并不会导致极端的底部-底部仍然留有一些空间。
I am having problems to reproduce you issue. 我在重现您的问题时遇到了问题。 If you can post all the relevant code (and maybe include a screenshot), we can give you better advice.
如果您可以发布所有相关代码(并可能包括屏幕截图),我们将为您提供更好的建议。 When I run the code below, pressing the button moves the scroll pane all the way down:
当我运行以下代码时,按下按钮将滚动窗格一直向下移动:
public class ScrollPaneBottom {
public static void main(final String[] args) {
new ScrollPaneBottom().test();
}
private void test() {
final JFrame frame = new JFrame("Move a scroll pane to the bottom");
frame.setBounds(100, 100, 800, 110);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final String[][] data = {{"One", "Hello"}, {"Une", "Bonjour"},
{"Ein", "Hallo"}, {"Uno", "Hola"}, {"Een", "Hallo"}};
final String[] columnNames = {"Number", "Word"};
final JTable table = new JTable(new DefaultTableModel(data, columnNames));
final JScrollPane scrollPane = new JScrollPane(table);
final JButton button = new JButton("Move to the bottom");
button.addActionListener(e -> {
final int maximum = scrollPane.getVerticalScrollBar().getMaximum();
scrollPane.getVerticalScrollBar().setValue(maximum);
});
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.