Adding a JTextPane to BorderLayout.SOUTH causes JS

2020-05-08 19:26发布

问题:

I have a JPanel which is contained within a JScrollPane. The JPanel has components added to it's NORTH, CENTER, WEST and SOUTH areas (BorderLayout).

When I add a JTextPane to the SOUTH position, the scroll pane scrolls to show the text. I do not want the scroll pane to move from its topmost position. How can I prevent this?

回答1:

Absent more details, you can try setting the default caret to NEVER_UPDATE, available since Java 5.

JTextPane jtp = new JTextPane();
DefaultCaret caret = (DefaultCaret) jtp.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);