Feb
15
2008

Java: Word Wrap for JLabels



Comments available as RSS 2.0

Why couldn't Sun have made JLabels automatically wrap when they're too wide to fit the container they're in? I'd been trying to get a custom component to automatically wrap for hours, then someone suggested a JTextPane in IRC. You can style the pane to look like a JLabel, and it automatically wraps.

JTextPane txtMyTextPane= new JTextPane();
            txtMyTextPane.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas aliquam sem et nunc vulputate aliquet. Duis sollicitudin. Vestibulum sit amet lacus a risus egestas nonummy. Donec eu odio id felis auctor lobortis. Cras id nisl vitae pede lacinia elementum. Suspendisse convallis leo. Donec elit. Quisque id mi tincidunt quam tristique posuere.");
            txtMyTextPane.setBackground(null);
            txtMyTextPane.setEditable(false);
            txtMyTextPane.setBorder(null);

Viewing 1 Comment

Trackbacks

close Reblog this comment
blog comments powered by Disqus