• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems resizing using a split pane divider.

 
Stevo Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

Here's what I'm working with:

1) I've got a collection of JTextAreas. Each concerns a separate item so I would like to maintain them as separate JTextAreas.
2) The JTextAreas are added to a JPanel using a vertical box layout. The desired effect is to have the JTextAreas positioned in a vertical column, expanding to fill the width of the JPanel.
3) The JPanel is set as the viewport view of a JScrollPane (using default scrollpane layout).
4) The JScrollPane is set as one of the children of a JSplitPane (using default split orientation).

My problem:

When I move the JSplitPane divider to give the JScrollPane more space, all of the JTextAreas expand horizontally to fill the new space and re-wrap their text correctly. When I move the JSplitPane divider to give the JScrollPane less space, the JTextAreas do not compress to fit within the new horizontal constraint. Instead, a horizontal scroll bar appears on the JScrollPane. I never want the user to have to scroll horizontally. Instead I want the JTextAreas to be compressed horizontally, forcing them to expand vertically, and a vertical scroll bar can appear if necessary. Setting the JScrollPane's horizontal scroll bar policy to NEVER did not solve the problem, it just hid the horizontal scroll bar so I could not scroll to see the content even if I wanted.

It seems to me that when the JScrollPane gets wider it resizes the JPanel to fill the space but when it gets smaller it does not. Any suggestions?

Thanks!
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try setting the preferred width of the JTextAreas to something really small. I think that setting the number of columns to 1 should do it.
 
Stevo Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First attempt: setColumns(1)

Result: did not do anything.


SecondAttempt: setPreferredSize( new Dimension( 0,0 ) )

Result: The JTextAreas now expand and contract horizontally as I adjust the JSplitPane divider and the text re-wraps correctly in both cases. This is desirable. Unfortunately the height of each JTextArea is now fixed. What I would like is for the height to adjust along with the width so that the JTextArea has enough area to display all of its text. Unfortunately there is not a setPreferredWidth( int i ) method.

Perhaps I should be using a spring layout on the JPanel instead of a vertical box layout? I've never used a spring layout before and the Java API says it was not designed for manual use. How hard is it to use?


Thanks so much for the feed back, I will keep trying!
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also create a subclass of JTextArea, and override getPreferredSize:
This way, the usual preferred height is used but the preferred width will always be 0.
 
Stevo Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Worked like a charm. Thanks so much for your help Rob!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!