I am trying to make chat panel, where message texts are shown by JLabel, I want tu make word wrap in it, i have inserted <html>...</html> in a label and sat preferredSize, after that, when i add many messages, scroll not works properly, there appears a lot of free space after text when i scroll down, as shown on this screenshot
can u tell me better way to do this ?
final class ChatFrame extends JPanel {
JLabel text;
JScrollPane scroll = new JScrollPane();
public ChatFrame(int width,int height) {
scroll.setPreferredSize(new Dimension(width, height));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
scroll.getViewport().add(this);
setBorder(new EmptyBorder(5, 5, 5, 5));
addMessage("<i>Connecting...</i>", false);
}
public void addMessage(String message, boolean right) {
text = new JLabel("<html>"+message+"</html>");
text.setOpaque(true);
text.setBackground(Color.lightGray);
text.setBorder(new EmptyBorder(5, 5, 5, 5));
add(text);
JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
sep.setMaximumSize(new Dimension(0, 3));
add(sep);
revalidate();
}
public Component getChatView() {
return scroll;
}
}