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);
Like this post? Tell someone!
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google
  • Technorati

Comments

  1. 1 Bill M.

    just ran into this with one of my first Netbeans apps …thanks for the tip
    (2nd send try / first errored out saying email IS required)

Leave a Comment

Login using OpenID or enter your details below to leave a comment.

OpenID
Anonymous


Comment