title: Swing JList with multiline text and dynamic height
body: <p>I already read/tried these posts but that didn't help:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/5632901/display-multiple-lines-within-a-jlist-cell">Display multiple lines within a Jlist cell</a></li>
  <li><a href="https://stackoverflow.com/questions/2726510/how-to-get-multiline-for-a-jlist-text">How to get multiline for a Jlist text?</a></li>
  <li><a href="https://stackoverflow.com/questions/5631048/problem-displaying-components-of-jlist">Problem displaying components of JList</a></li>
  </ul>
  
  <p>What I need is a <code>ListCellRenderer</code> which returns a panel with an icon on the left and a text of dynamic length on the right (like in any forum: on the left a user avatar, on the right the post text). The texts are <strong>NOT</strong> known to me, so I can't set a fixed cell height. Further, the text length differs from list cell to list cell. So every list cell needs its own height depending on the length of the text. Actually a really common layout ... but not for Swing. The cell height just doesn't expand according to the text length.</p>
  
  <p>I already read almost any post out there about dynamic cell heights and multiline texts in <code>JList</code>, but couldn't find a solution. So I decided to give a small SSCCE. Please give me a hint on how to achieve what I described or please fix my code if you think it's easy.</p>
  
  <p>Thanks</p>
  
  <p>Here is ths SSCCE:</p>
  
  <pre><code>public class MultiLineList extends JFrame
  {
  
      private static final long serialVersionUID = 1L;
  
      public static void main(final String[] args)
      {
          new MultiLineList();
      }
  
      private MultiLineList()
      {
          setTitle("MultiLineList");
          setSize(800, 450);
          setResizable(true);
          setVisible(true);
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          this.getContentPane().setLayout(new BorderLayout());
  
          final DefaultListModel model = new DefaultListModel();
          model.addElement("This is a short text");
          model.addElement("This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. ");
          model.addElement("This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. ");
  
          final JList list = new JList(model);
          list.setCellRenderer(new MyCellRenderer());
  
          this.add(list);
  
          this.getContentPane().invalidate();
          this.getContentPane().validate();
  
      }
  
      public class MyCellRenderer extends DefaultListCellRenderer
      {
          @Override
          public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean hasFocus)
          {
  
              final String text = (String) value;
  
              //create panel
              final JPanel p = new JPanel();
              p.setLayout(new BorderLayout());
  
              //icon
              final JPanel IconPanel = new JPanel(new BorderLayout());
              final JLabel l = new JLabel("icon"); //&lt;-- this will be an icon instead of a text
              IconPanel.add(l, BorderLayout.NORTH);
              p.add(IconPanel, BorderLayout.WEST);
  
              //text
              final JTextArea ta = new JTextArea();
              ta.setText(text);
              ta.setLineWrap(true);
              ta.setWrapStyleWord(true);
              p.add(ta, BorderLayout.CENTER);
  
              return p;
  
          }
      }
  
  }
  </code></pre>