Scroll JScrollPane to bottom

前端 未结 12 938
庸人自扰
庸人自扰 2020-11-30 02:18

I need to scroll a JScrollPane to the bottom. The JScrollPane contains a JPanel, which contains a number of JLabel\'s.

To scroll to the top, I just do:



        
12条回答
  •  青春惊慌失措
    2020-11-30 02:51

    Instead of setViewPosition(), I usually use scrollRectToVisible(), described in How to Use Scroll Panes. You could use the result of an appropriate label's getBounds() for the required Rectangle.

    Addendum: @Matt notes in another answer, "If you use the following code after you output text to the text area in the scrollpane, it will automatically focus on the bottom of the text area."

    In the particular case of a JTextComponent, also consider using the setUpdatePolicy() method of DefaultCaret to ALWAYS_UPDATE, illustrated here.

提交回复
热议问题