Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 562
niteen22 Avatar asked Oct 15 '12 11:10

niteen22


1 Answers

  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.

like image 87
Mikle Garin Avatar answered Oct 23 '22 09:10

Mikle Garin