I have JLabel that displays a dynamic text. This text can be very long or short. I want to wrap text and I'm trying it this way:
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.setMaximumSize(new Dimension(500, 150));
....
lblInfo=new JLabel();
lblInfo.setText("<html><b>Q: "+ infoObj.getText()+"</b></html>");
...
panel1.add(lblInfo);
But this doesn't seem to work. When a long text comes, this JLabel just goes out of the screen (beyond the size of my panel) and I can only see the end of it.
I found some solutions on Stack Overflow using JTextField instead of label. But due to some requirements in my project, I have to use JLabel itself in my case.
When a long text comes, this JLabel just goes out of the screen
Yes, the text will only wrap when you have a <br>
in the actual text. The <br>
at the start of the text does nothing.
I found some solutions on stackoverflow using JTextField instead of label
I doubt that. A JTextField
ALWAYS displays text on a single line.
The suggestion you will find in the forum is to use a JTextArea
with wrapping:
JTextArea textArea = new JTextArea(5, 20);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.