6

How can text like "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" which exceeds the width of the JLabel be wrapped? I have tried enclosing the text into html tags but no luck. Please give your suggestions.

splungebob
  • 5,217
  • 2
  • 18
  • 44
Nadeem
  • 165
  • 1
  • 1
  • 14
  • the possible duplicate fails to provide solution when the word has no space char in it. I mean when there is only one word that exceeds the width of jlabel, what should be done in that case? – Nadeem Oct 17 '14 at 08:09

2 Answers2

15

A common approach is to not use a JLabel and instead use a JTextArea with word-wrap and line-wrap turned on. You could then decorate the JTextArea to make it look like a JLabel (border, background color, etc.). [Edited to include line-wrap for completeness per DSquare's comment]

Another approach is to use HTML in your label, as seen here. The caveats there are

  1. You may have to take care of certain characters that HTML may interpret/convert from plain text

  2. Calling myLabel.getText() will now contain HTML (with possibly escaped and/or converted characters due to #1

EDIT: Here's an example for the JTextArea approach:

enter image description here

import javax.swing.*;

public class JLabelLongTextDemo implements Runnable
{
  public static void main(String args[])
  {
    SwingUtilities.invokeLater(new JLabelLongTextDemo());
  }

  public void run()
  {
    JLabel label = new JLabel("Hello");

    String text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
//        String text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + 
//                      "quick brown fox jumped over the lazy dog.";

    JTextArea textArea = new JTextArea(2, 20);
    textArea.setText(text);
    textArea.setWrapStyleWord(true);
    textArea.setLineWrap(true);
    textArea.setOpaque(false);
    textArea.setEditable(false);
    textArea.setFocusable(false);
    textArea.setBackground(UIManager.getColor("Label.background"));
    textArea.setFont(UIManager.getFont("Label.font"));
    textArea.setBorder(UIManager.getBorder("Label.border"));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(label, BorderLayout.NORTH);
    frame.getContentPane().add(textArea, BorderLayout.CENTER);
    frame.setSize(100,200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
Community
  • 1
  • 1
splungebob
  • 5,217
  • 2
  • 18
  • 44
  • He specifically doesn't want a word wrap behaviour but a chracter one. Which means that a JTextArea with `setLineWrap(true)` but `setWrapStyleWord(false)` would work. – DSquare Oct 17 '14 at 14:21
  • @DSquare: You are correct about the line-wrap. However, using word-wrap would be also necessary so that all other words that aren't longer that the label length (which likely would be most of them) are still wrapped appropriately. Without it, they would break unceremoniously and the end of the label length, which is likely undesired (but not mentioned in the OP). Set word-wrap to false in my example to see what I mean. – splungebob Oct 17 '14 at 14:50
  • I agree that word-wrap is generally desirable, but that's not the issue here. Since word-wrap overrides the default character-wrap behavior it is specifically not desired by OP. Please set `String text = "aaa...aaa"` (without spaces, the case in question), and see how your code doesn't solve the issue. – DSquare Oct 17 '14 at 15:06
  • It works for me (see pic). Or perhaps I'm not understanding the requirement. – splungebob Oct 17 '14 at 15:11
  • Sorry, you are right, I thought it overrided the default behavior but it is actually an extension. – DSquare Oct 17 '14 at 15:15
2

Use JLabel with the html properties to justify the text.

        JLabel txtTitulo = new JLabel(
            String.format("<html><body style=\"text-align: justify;  text-justify: inter-word;\">%s</body></html>","Long text example aaaaaa bbbbbb aaaaaaaaaaaaaaaaaaaa.");
    );

Doc: https://www.w3schools.com/cssref/css3_pr_text-justify.asp

Ronald Coarite
  • 3,431
  • 23
  • 25