This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes line wrapping and scrolling in a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "line wrapping and scrolling in a JPanel" Watch "line wrapping and scrolling in a JPanel" New topic

line wrapping and scrolling in a JPanel

Mark Vermeersch

Joined: May 11, 2004
Posts: 17

I've got an empty JPanel to which I want to add an (unknown) number of JLabels. Each JLabel has a text which is just one word. When my program is adding JLabels to the JPanel, I want it to have the same behaviour as a normal text editor: when a line is full, the next line gets filled (line wrapping).

If I just keep adding JLabels to a JPanel of a fixed size with .add(), there's no line wrapping. I tried it by giving the JPanel a fixed size, flowLayout and nesting it within a JScrollPane, but it keeps filling one long line, instead of jumping to the next one.

Any obvious solutions??
[ October 18, 2004: Message edited by: Mark Vermeersch ]
Matthew Taylor

Joined: Jun 13, 2004
Posts: 110
Instead of using the Component method .setSize() for the JPanel, try using the JComponent method .setPreferredSize(). Hope that helps.

Grails Consultant
Mike Rainville
Ranch Hand

Joined: May 29, 2004
Posts: 36
Have looked at javax.swing.JTextArea?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
If it's just one big word requiring wrapping, something like this maybe

Mark Vermeersch

Joined: May 11, 2004
Posts: 17
Originally posted by Mike Rainville:
Have looked at javax.swing.JTextArea?

Sure, but I want this text behaviour for all the components inside the JPanel: labels, buttons,... everything should behave as a text that performs line wrapping.

But I think I can solve it by overwriting the getPreferredSize() method of the JPanel I'm using.

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: line wrapping and scrolling in a JPanel
jQuery in Action, 3rd edition