java scrollpane_java-将JScrollPane滚动到底部

java-将JScrollPane滚动到底部

我需要将JScrollPane滚动到底部。 JScrollPane包含一个JPanel,其中包含许多JLabel。

要滚动到顶部,我只需要:

scrollPane.getViewport().setViewPosition(new Point(0,0));

但是如何精确滚动到最底端? (太远了,它抖动)

Matt asked 2020-07-20T01:27:11Z

12个解决方案

104 votes

JScrollBar vertical = scrollPane.getVerticalScrollBar();

vertical.setValue( vertical.getMaximum() );

camickr answered 2020-07-20T01:27:17Z

36 votes

经过数小时的尝试,尝试使用scrollRectToVisible()方法找到一个答案以外的其他答案,然后我成功了。 我发现,如果在将文本输出到滚动窗格中的文本区域后使用以下代码,它将自动聚焦在文本区域的底部。

textArea.setCaretPosition(textArea.getDocument().getLength());

所以,至少对我来说,我的打印方法是这样的

public void printMessage(String message)

{

textArea.append(message + endL);

textArea.setCaretPosition(textArea.getDocument().getLength());

}

Decesus answered 2020-07-20T01:27:42Z

24 votes

scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

e.getAdjustable().setValue(e.getAdjustable().getMaximum());

}

});

Peter Saitz answered 2020-07-20T01:27:57Z

16 votes

我通常使用2974110383042200200576代替setUpdatePolicy(),如如何使用滚动窗格中所述。 您可以将适当标签DefaultCaret的结果用于所需的ALWAYS_UPDATE。

附录:@Matt在另一个答案中指出:“如果在将文本输出到滚动窗格的文本区域后使用以下代码,它将自动聚焦在文本区域的底部。”

在setUpdatePolicy()的特定情况下,还可以考虑使用此处说明的DefaultCaret至ALWAYS_UPDATE的setUpdatePolicy()方法。

trashgod answered 2020-07-20T01:28:27Z

11 votes

我修改了Peter Saitz的代码。 此版本在向下滚动后使滚动条继续工作。

private void scrollToBottom(JScrollPane scrollPane) {

JScrollBar verticalBar = scrollPane.getVerticalScrollBar();

AdjustmentListener downScroller = new AdjustmentListener() {

@Override

public void adjustmentValueChanged(AdjustmentEvent e) {

Adjustable adjustable = e.getAdjustable();

adjustable.setValue(adjustable.getMaximum());

verticalBar.removeAdjustmentListener(this);

}

};

verticalBar.addAdjustmentListener(downScroller);

}

Matthias Braun answered 2020-07-20T01:28:47Z

1 votes

如果您的JScrollPane仅包含JTextArea,则:

JScrollPane.getViewport().setViewPosition(new Point(0,JTextArea.getDocument().getLength()));

Gonzalo answered 2020-07-20T01:29:07Z

1 votes

没有答案对我有用。 由于某种原因,即使我重新验证了所有内容,我的JScrollPane也没有滚动到最底端。

这对我有用:

SwingUtilities.invokeLater(() -> {

JScrollBar bar = scroll.getVerticalScrollBar();

bar.setValue(bar.getMaximum());

});

David Rochin answered 2020-07-20T01:29:31Z

0 votes

// Scroll to bottom of a JScrollPane containing a list of Strings.

JScrollPane scrollPane;

DefaultListModel listModel;

JList list;

listModel = new DefaultListModel();

list = new JList(listModel);

list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

list.setLayoutOrientation(JList.VERTICAL);

list.setVisibleRowCount(-1); // -1 = display max items in space available

scrollPane = new JScrollPane(list);

scrollPane.setPreferredSize(new Dimension(200, 50));

// Append text entries onto the text scroll pane.

listModel.addElement("text entry one");

listModel.addElement("text entry two");

listModel.addElement("text entry three");

listModel.addElement("text entry four");

// Get the index of the last entry appended onto the list, then

// select it, and scroll to ensure it is visible in the vewiport.

int lastNdx = listModel.getSize() - 1;

list.setSelectedIndex(lastNdx);

list.ensureIndexIsVisible(lastNdx);

JPanel panel = new JPanel();

panel.add(scrollPane);

gghptg answered 2020-07-20T01:29:46Z

0 votes

我尝试了几种方法。 有些使用bar.setValue(bar.getMaximum()),但最大值始终为100,而bar的范围将远远超过100。有些使用textArea,但不适用于JScrollPane中只有JTable的情况。 我认为一种方法可能愚蠢但有效。

JScrollBar bar=jsp.getVerticalScrollBar();

int x=bar.getValue();

for(int i=100;i<2000000000;i+=100)

{

bar.setValue(i);

if(x==bar.getValue())

break;

x=bar.getValue();

}

GAOLE LI answered 2020-07-20T01:30:07Z

0 votes

我想将我的发现贡献给这个问题,因为我今天需要一个答案,而这里没有一个解决方案对我有用,但是我终于找到了一个可行的解决方案。 我将JList对象包装在JScrollPane中,在将所有元素添加到DefaultListModel之后,我想滚动到最后一个项目。 本质上是这样的:

JList list = new JList();

DefaultListModel listModel = new DefaultListModel();

JScrollPane listScroller = new JScrollPane(list);

public void populateList()

{

//populate the JList with desired items...

list.ensureIndexIsVisible(listModel.indexOf(listModel.lastElement()));

}

我尝试了此处列出的所有解决方案,但似乎没有任何效果。 我在进行实验时发现了这个,并且效果很好。 我以为我会把它留在这里,以免对别人有帮助。

hektik answered 2020-07-20T01:30:32Z

0 votes

ScrollBar vertical = scrollPane.getVerticalScrollBar();

vertical.setValue( vertical.getMaximum() - 1 );

只需将值设置为:vertical.getMaximum() - 1

pereca answered 2020-07-20T01:30:52Z

-2 votes

如果您查看JTextArea文档

public void select(int selectionStart, int selectionEnd)

在指定的开始和结束位置之间选择文本。此方法设置所选文本的开始和结束位置,从而限制了开始位置必须大于或等于零的限制。 结束位置必须大于或等于开始位置,并且小于或等于文本组件文本的长度。

如果调用者提供的值不一致或超出范围,则该方法将无提示地强制执行这些约束,并且不会失败。 具体来说,如果开始位置或结束位置大于文本长度,则将其重置为等于文本长度。 如果开始位置小于零,则将其重置为零;如果结束位置小于开始位置,则将其重置为开始位置。

因此,简单的解决方案是jTextArea.select(Integer.MAX_VALUE,0); 让Java对其进行分类!

Ian Powell answered 2020-07-20T01:31:30Z

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值