title: Converting List<ClassName> to array object to set as JComboBoxModel
body: <p>I am designing classes based on DAO Pattern. </p>
  
  <p>I have 3 classes and 1 GUI Form.</p>
  
  <pre><code>public interface SchoolYearDao {
      List&lt;SchoolYear&gt; getAllSchoolYearInfo();
      List&lt;SchoolYear&gt; getAllSchoolYearStart();
      List&lt;SchoolYear&gt; getAllSchoolYearEnd();
      List&lt;SchoolYear&gt; getSchoolYearById(int aSchoolYearId);
      int getSchoolYearId(SchoolYear schoolyear);
      boolean addSchoolYear(SchoolYear schoolyear);
  
  }
  
  public class SchoolYear {
   //setters and getters
  }
  
  public class SchoolYearDaoImpl implements SchoolYearDao{
  @Override
      public List&lt;SchoolYear&gt; getAllSchoolYearStart() {
          List&lt;SchoolYear&gt; listOfSchoolYearStart = new ArrayList&lt;&gt;();
          SchoolYear mySchoolYear = new SchoolYear();
          String SQL = "{CALL getAllSchoolYearInfo()}";
  
          try(Connection con = DBUtil.getConnection(DBType.MYSQL);
                  CallableStatement cs = con.prepareCall(SQL);) {
              try(ResultSet rs = cs.executeQuery();){
                  while(rs.next()){
                      mySchoolYear.setStart(rs.getInt("yearFrom"));
                  }
                  listOfSchoolYearStart.add(mySchoolYear);
              }
          } catch (SQLException e) {
              JOptionPane.showMessageDialog(null,e.getMessage());
          }
          System.out.println(listOfSchoolYearStart);
          return listOfSchoolYearStart;
      }
  }
  </code></pre>
  
  <p>The problem is with the GUI.</p>
  
  <pre><code>public class SchoolYearGUI extends javax.swing.JPanel {
      public SchoolYearGUI() {
          initComponents();
  
          schoolYearStartJcbx.setModel(new DefaultComboBoxModel(schoolyear.getAllSchoolYearInfo().toArray());
          schoolYearEndJcbx.setModel(new DefaultComboBoxModel(schoolyear.getAllSchoolYearEnd().toArray()));
      }
  }
  </code></pre>
  
  <p>I can't get the years to show correctly. I get this. </p>
  
  <p><a href="https://i.stack.imgur.com/xa7a1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xa7a1.png" alt="enter image description here"></a></p>
  
  <p>Instead of the actual integer numbers 2015,2016,2017 and so on...</p>
  
  <p>I research online and found similar problems but most of them were not using a list of <code>class</code> as <code>List&lt;SchoolYear&gt;</code>. In this case, <strong>"SchoolYear"</strong> is the name of class.</p>
  
  <p>I used <code>toArray();</code> and tried <code>Arrays.toString(array);</code> but can't get it right.
  I thought I'd change the return type to <code>DefaultComboBoxModel</code> of <code>getAllSchoolYearStart()</code> method but I realized I have to keep my <code>List&lt;SchoolYear&gt;</code> as <strong>return type</strong> in case I need to use the result set as model for <code>JTables</code> etc..</p>
  
  <p>So, I want to just stick with <code>List&lt;SchoolYear&gt;</code> as <strong>return type</strong>. (If it's a good idea?)</p>
  
  <p>What is the best way to get the actual value? </p>
  
  <p>Thanks in advance.</p>
  
  <p>=============== Solution ==============================</p>
  
  <p>Thanks to <code>MadProgrammer</code> for the advice and to other answerers.</p>
  
  <p>So I studied the <code>listcellrenderer</code> overnight and finally got the basic idea of how to use it. </p>
  
  <pre><code>public MainFrame() {
          initComponents();
          SchoolYearDaoImpl sy = new SchoolYearDaoImpl();
          DefaultComboBoxModel model = new DefaultComboBoxModel(sy.getAllSchoolYearStart().toArray());
          jcmbSchoolYearStart.setModel(model);
          jcmbSchoolYearStart.setRenderer(new DefaultListCellRenderer() {
              @Override
              public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                  super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                  if(value instanceof SchoolYear){
                      SchoolYear schoolyear = (SchoolYear) value;
                      setText(""+schoolyear.getStart());
                  }
                  return this;
              }
          } );
      }
  </code></pre>
  
  <p>I overridden the <code>getListCellRendererComponent</code> and created an <code>if-statement</code> to test if value is an instance of my class which is "SchoolYear" Then I cast whatever the raw <code>value</code> is to <strong>SchoolYear</strong> then used the getter of SchoolYear model, <code>getStart()</code> to get the value stored in the list.</p>
  
  <p>I'm now moving the renderer to an external class file in my project.</p>
  
  <p><a href="https://i.stack.imgur.com/3pYri.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3pYri.jpg" alt="enter image description here"></a></p>