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.