How to make JTextPane wrapped in JScrollPane shrink to fit the text

柔情痞子 提交于 2019-12-24 10:29:00

问题


I have a JTextPane with HTML text.

I used GroupLayout (using WindowBuilder).

I've set the minimum size of my JFrame to 800x600 so the user cannot make it smaller than that.

The app has a big scrolling JPanel the size of the entire window. The top part of the panel is taken up by a JTextPane wrapped in JScrollPane. I have disabled the scroll bars and sized the JScrollPane to make the entire text visible.

In group layout the JScrollPane is set to stay constant vertically, but size horizontally.

My issue is that when the user makes the window larger the JScrollPane also expands, but now there is a big white space left at the bottom of the text pane. Is there a way that I can make JTextPane shrink to fit its contents.

Also if you suggest a different layout, I would be willing to try it.


回答1:


I used this TextPanePerfectSize example from @camickr to solve a similar problem. The example uses validate() and pack() to adjust to the preferred size. You might be able to adapt it to your situation.




回答2:


Take a look at SpringLayout. It gives you far more control over the positioning of components. Look at the SpringLayout tutorial if you get stuck.

The trick in your case is to bind the bottom (south) of your JScrollPane to the top (north) of the screen.



来源:https://stackoverflow.com/questions/13132375/how-to-make-jtextpane-wrapped-in-jscrollpane-shrink-to-fit-the-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!