10

Whenever JLabel contains text in tag it applies line wrap automatically (it seems). My requirement is line wrap should always be disabled for label, no matter what text it contains. I can not use JTextArea in my renderer due to legacy reasons.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
niteen22
  • 351
  • 3
  • 13

1 Answers1

18
  1. You can use <nobr></nobr> tag around the HTML content you don't want to be wrapped
  2. Simple non-HTML content will never be wrapped inside the JLabel

Here is an example:

public static void main ( String[] args )
{
    JFrame frame = new JFrame ();
    frame.setLayout ( new BorderLayout () );

    final String html = "<html><body><nobr>CMV Antigenemia Stat X 2.0 dose(s)</nobr></body></html>";
    final String simple = "<html><body>CMV Antigenemia Stat X 2.0 dose(s)</body></html>";

    JTable table1 = new JTable ( new String[][]{ { html, html, html, html, html } }, new String[]{ html, html, html, html, html } );
    table1.setRowHeight ( 50 );
    frame.add ( table1, BorderLayout.NORTH );

    JTable table2 = new JTable ( new String[][]{ { simple, simple, simple, simple, simple } },
            new String[]{ simple, simple, simple, simple, simple } );
    table2.setRowHeight ( 50 );
    frame.add ( table2, BorderLayout.CENTER );

    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

As you can see - in the 1st table HTML content is not getting wrapped.

Mikle Garin
  • 10,083
  • 37
  • 59
  • Tried this , but did not worked. I am adding this label to JTable - basically my renderer component is JLabel. – niteen22 Oct 16 '12 at 12:13
  • What exactly did not work? How did you insert `` tag around your HTML code? Please add some code into your question that shortly describes the problem. – Mikle Garin Oct 16 '12 at 15:24
  • I get following text in getTableCellRendererComponent method of my renderer - "CMV Antigenemia Stat X 2.0 dose(s)". Component returned here has to be JLabel due to legacy reasons (Problem could have been solved by textarea. – niteen22 Oct 17 '12 at 11:32
  • @niteen22 you are doing something wrong - check the code i have added in my answer. HTML content you have posted in the last comment doesn't get wrapped (and it shouldn't) - just try resizing the frame. So what is the problem? – Mikle Garin Oct 17 '12 at 11:53