How to make JTextPane wrapped in JScrollPane shrin

2019-08-12 00:20发布

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.

2条回答
祖国的老花朵
2楼-- · 2019-08-12 01:10

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.

查看更多
在下西门庆
3楼-- · 2019-08-12 01:10

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.

查看更多
登录 后发表回答