Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, remember, <code>CellRenderers</code> are used as "rubber stamps" in Swing. This is done to save memory and help increase performance. This allows components, like <code>JList</code>, to maintain a single reference to the renderer but still paint all the items in the list.</p> <p>This means, that when ever <code>getListCellRendererComponent</code> is called, the renderer will be in the same state it was last it was called.</p> <p>From what I can tell from your code, this will mean that each time the renderer is called, you add ANOTHER checkbox, ANOTHER separator ANOTHER panel and lots more labels...</p> <p>Secondly of all, with renderers, try not to create anything in the the <code>getListCellRendererComponent</code> where possible, this is just increasing you memory usage (part of your code may not have a choice).</p> <p>Thirdly, <code>JList</code> does not support editors, renderers are not editors, for the reasons I've stated before.</p> <p>Forthly. Try removing the content from the "body" portion of the panel before adding all your elements to it.</p> <pre><code>// Don't use Container, use JPanel instead, mixing heavy and light weight components // is never a good idea :P public class GAListRendererAdv extends JPanel implements ListCellRenderer { private static final long serialVersionUID = 1L; private JPanel pnl = new JPanel(); private Random rnd = new Random(); private JCheckBox checkBox; public GAListRendererAdv() { setLayout(new BorderLayout(0, 0)); checkBox = new JCheckBox(); add(checkBox, BorderLayout.NORTH); add(new JSeparator(), BorderLayout.CENTER); pnl = new JPanel(new GridLayout(0, 1, 0, 0)); pnl.setBorder(new EmptyBorder(0, 35, 0, 0)); add(pnl BorderLayout.SOUTH); // Don't forget to add the panel :P } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { pnl.removeAll(); int rndNum = rnd.nextInt(5) + 1; for (int i = 0; i &lt; rndNum; i ++) { JLabel lblNewLabel2 = new JLabel("New label"); pnl.add(lblNewLabel2); } return this; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload