*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

line wrapping and scrolling in a JPanel

Mark Vermeersch
Greenhorn

Joined: May 11, 2004
Posts: 17
Hello,

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??
Thanks
Mark
[ October 18, 2004: Message edited by: Mark Vermeersch ]
Matthew Taylor
Rancher

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
http://dangertree.net
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
Greenhorn

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.

Mark
 
 
subject: line wrapping and scrolling in a JPanel
 
Similar Threads
JScrollPane and FlowLayout
Making clickable links using HTML in a JLabel
Difficulities in adding loaded image to JPanel
JTextPane viewable area
problem w/ MouseListener and JLabel