title: How to get the size of a JList?
body: <p>I'm trying to get the width of a JList from within its cell renderer getListCellRendererComponent() method.</p>
  
  <p>The estrange behaviour I'm getting is that as far as it is being enlarged I get correct widths but for sizes smaller than the original one I always get the width it had when it was first drawn.</p>
  
  <p>This is so for list.getWidth(), list.getSize(), list.getBounds() and list.getCellBounds().
  Taking width of the list's containing ScrollPanel with parent.getxxxxx() works as expected.</p>
  
  <p>By the way there's no min/max/preferredSize set on this JList.</p>
  
  <p>At this point I'm at a loss..</p>
  
  <p><strong>EDIT:</strong><br>
  So as it seems the containing ScrollPanel is not being revalidated on resize. Shouldn't revalidate() be called automatically? Mind this is not anything done programatically but just a mouse Frame resize performed by the user.</p>
  
  <p><strong>EDIT 2:</strong><br>
  Yeah, well, JList's geometry has some complex intrincacies which at the end of the day are not related at all with actual screen real state. Anyone interested on the later better get at the containing ScrollPane's ViewPort and query that (getWidth, getHaight, getSize, getBounds).</p>
  
  <p><strong>EDIT 3</strong><br>
  Here is an SSCCE as requested. Not really short though as I wanted to keep Netbeans' layout code just in case it has some bearing on the issue. With this I get a JList width of 667 which seems to be somewhat related to longest item's length (at 667 it starts showing ellipsis at the end).
  As soon as viewport gets 667 wide, jlists width starts tracking viewport's width. When shrinking this tracking stops at 667 and remains at that.</p>
  
  <pre><code>package com.dafquest.operator.mvc;
  
  import java.awt.Component;
  import javax.swing.DefaultListCellRenderer;
  import javax.swing.JLabel;
  import javax.swing.JList;
  
  public class JListSSCCE extends javax.swing.JFrame {
  
      public JListSSCCE() {
          initComponents();
      }
  
      @SuppressWarnings("unchecked")
      private void initComponents() {
  
          jPanel1 = new javax.swing.JPanel();
          jScrollPane1 = new javax.swing.JScrollPane();
          jList1 = new javax.swing.JList();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
          setName("Form");
  
          jPanel1.setName("jPanel1");
  
          jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
          jScrollPane1.setName("jScrollPane1");
  
          jList1.setModel(new javax.swing.AbstractListModel() {
              String[] strings = { "This is this Jlist's Item 1", "A shorter Item 2", "For some reason Item 3 seems to be longer", "Certainly issue whith item 3 must be a mirage, Item 4 *is* a really long item", "Item 5" };
              public int getSize() { return strings.length; }
              public Object getElementAt(int i) { return strings[i]; }
          });
          jList1.setCellRenderer(new MyCellRenderer());
          jList1.setName("jList1");
          jScrollPane1.setViewportView(jList1);
  
          javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
          jPanel1.setLayout(jPanel1Layout);
          jPanel1Layout.setHorizontalGroup(
              jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
          );
          jPanel1Layout.setVerticalGroup(
              jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
          );
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addContainerGap()
                  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addContainerGap())
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addContainerGap()
                  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addContainerGap())
          );
  
          pack();
      }                        
  
      public static void main(String args[]) {
          try {
              for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                  if ("Nimbus".equals(info.getName())) {
                      javax.swing.UIManager.setLookAndFeel(info.getClassName());
                      break;
                  }
              }
          } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(JListSSCCE.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
  
          java.awt.EventQueue.invokeLater(new Runnable() {
              @Override
              public void run() {
                  new JListSSCCE().setVisible(true);
              }
          });
      }
  
      private javax.swing.JList jList1;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JScrollPane jScrollPane1;
  
      private class MyCellRenderer extends DefaultListCellRenderer {
  
          @Override
          public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
  
              JLabel comp = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
              String widths = "(SPViewPort: " + list.getParent().getWidth()
                      + ", Jlist: " + list.getWidth()
                      + ", Cell: " + comp.getWidth()
                      + ", CellPref: " + comp.getPreferredSize().width + ") ";
              comp.setText(widths + comp.getText());
  
              // Lets see after adding text, might be that JList width is equal
              // tho longest item's preferred width?
              widths = "(SPViewPort: " + list.getParent().getWidth()
                      + ", Jlist: " + list.getWidth()
                      + ", Cell: " + comp.getWidth()
                      + ", CellPref: " + comp.getPreferredSize().width + ") ";
              System.out.println(widths);
  
              return comp;
          }
      }
  }
  </code></pre>