Java tutorial
import java.awt.Component; import javax.swing.DefaultListCellRenderer; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; public class Main { public static void main(String[] args) { String[] names = { "John Smith", "this is a test", "this is a test. this is a test. " }; MyCellRenderer cellRenderer = new MyCellRenderer(80); JList<String> list = new JList<>(names); list.setCellRenderer(cellRenderer); JScrollPane sPane = new JScrollPane(list); JPanel panel = new JPanel(); panel.add(sPane); JOptionPane.showMessageDialog(null, panel); } } class MyCellRenderer extends DefaultListCellRenderer { public static final String HTML_1 = "<html><body style='width: "; public static final String HTML_2 = "px'>"; public static final String HTML_3 = "</html>"; private int width; public MyCellRenderer(int width) { this.width = width; } @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { String text = HTML_1 + String.valueOf(width) + HTML_2 + value.toString() + HTML_3; return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus); } }