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.
How to disable linewrap in JLabel even for <html> text
Asked Answered
- You can use
<nobr></nobr>
tag around the HTML content you don't want to be wrapped - 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.
Tried this , but did not worked. I am adding this label to JTable - basically my renderer component is JLabel. –
Cinchona
What exactly did not work? How did you insert
<nobr>
tag around your HTML code? Please add some code into your question that shortly describes the problem. –
Tedie I get following text in getTableCellRendererComponent method of my renderer - "<html><body><nobr>CMV Antigenemia Stat X 2.0 dose(s)</nobr></body></html>". Component returned here has to be JLabel due to legacy reasons (Problem could have been solved by textarea. –
Cinchona
@Cinchona 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? –
Tedie
© 2022 - 2024 — McMap. All rights reserved.
JLabel
is always single-lined unless it uses html-rendering – Ebonee