niteen22
niteen22

Reputation: 361

How to disable linewrap in JLabel even for <html> text

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.

Upvotes: 10

Views: 2516

Answers (1)

Mikle Garin
Mikle Garin

Reputation: 10143

  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.

Upvotes: 18

Related Questions