2

Please have a look at the following code

 JLabel menuItemTitle =  new JLabel("How to Find Waist");
     JLabel  menuItemDescription = new JLabel("Take the Neck measurement with your shoulders down and relaxed.\n Looking straight ahead rap the tape around the smallest point, below the Larynx, and sloping down slightly toward the front.\nTry a few spots to get the lowest number, and where it feels the most natural.");

        JPanel northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        northPanel.add(menuItemTitle);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new FlowLayout());
        centerPanel.add(menuItemDescription);


        JDialog dialog = new JDialog();
        dialog.setLayout(new BorderLayout());
        dialog.add(northPanel,"North");
        dialog.add(centerPanel,"Center");

                dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                dialog.setLocationRelativeTo(null);
                dialog.pack();
                dialog.setVisible(true);

I have added the required escape characters but it is not breaking the line. How can I add LineBreaks in JLabels? I ahve tried the above using HTML as well. But that also failed. please help.

update

html version

menuItemDescription = new JLabel("<html>Take the Neck measurement with your shoulders down and relaxed.</br> Looking straight ahead rap the tape around the smallest point, below the Larynx, and sloping down slightly toward the front.</br>Try a few spots to get the lowest number, and where it feels the most natural.</html>");
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • Can you post the HTML version, because that should work. – Goibniu Jan 02 '13 at 14:30
  • Duplicate of: http://stackoverflow.com/questions/1090098/newline-in-jlabel – Aleksander Blomskøld Jan 02 '13 at 14:30
  • 1
    You don't have a
    in the html version, you have
    – Goibniu Jan 02 '13 at 14:32
  • couldn't be better by using one of built-in methods for [Html and style='text-align', have to use SwingUtilities.LayoutLabelXxx to avoiding set PreferredSize manually](http://stackoverflow.com/a/14105694/714968) – mKorbel Jan 02 '13 at 14:33
  • @Goibniu: Great. Nice Catch. Thank you :) . Please provide your comment as the answer. I will select it – PeakGen Jan 02 '13 at 14:36
  • @mKorbel: Thanks for the comment. I will have a look at it and follow it in original development. This is just for a quick Mockup :) – PeakGen Jan 02 '13 at 14:37
  • @Sepala no worries, it's amazing how often you can get code blindness that can be quickly spotted by others. No need for a post, you can accept Upendra's answer. – Goibniu Jan 02 '13 at 14:52

1 Answers1

5

You can use HTML code inside your JLabel in this way

JLabel label = new JLabel("<html>Welcome to<br>Stackoverflow</html>");
  • 1
    Use `style='width: 120px;'` in the body for auto line-wrap. See [this answer](http://stackoverflow.com/questions/7861724/is-there-some-word-wrap-property-of-jlabel-exist/7861833#7861833) for details & demo. – Andrew Thompson Jan 02 '13 at 23:39