Array not showing in Jlist but filled in console

Posted by OVERTONE on Stack Overflow See other posts from Stack Overflow or by OVERTONE
Published on 2010-04-23T17:11:27Z Indexed on 2010/04/23 17:13 UTC
Read the original article Hit count: 238

Filed under:
|
|
|

Hey there. been a busy debugger today. ill give ths short version.

ive made an array list that takes names from a database. then i put the contents of the arraylist into an array of strings. now i want too display the arrays contents in a JList.

the weird thing is it was working earlier. and ive two methods. ones just a little practice too make sure i was adding to the Jlist correctly. so heres the key codes.

this is the layout of my code.

variables constructor methods

in my variables i have these 3 defined

String[] contactListNames = new String[5];
ArrayList<String> rowNames = new ArrayList<String>();
JList contactList = new JList(contactListNames);

simple enough.

in my constructor i have them again.

contactListNames = new String[5];
    contactList = new JList(contactListNames);
//i dont have the array list defined though.

 printSqlDetails();
// the prinSqldetails was too make sure that the connectionw as alright. and its working fine.

     fillContactList();
 // this is the one thats causing me grief. its where all the work happens.

   // fillContactListTest();
// this was the tester that makes sure its adding to the list alright.

heres the code for fillContactListTest()

  public void fillContactListTest()
 {
  for(int i = 0;i<3;i++)
  {
   try
   {
    String contact;
    System.out.println(" please fill the list at index "+ i);
    Scanner in = new Scanner(System.in);
    contact = in.next();
    contactListNames[i] = contact;
    in.nextLine();
   }
   catch(Exception e)
   {
 e.printStackTrace();   
   }
  }
 }

heres the main one thats supposed too work.

public void fillContactList()
       {
           int i =0;
           createConnection();
           ArrayList<String> rowNames = new ArrayList<String>();
           try
           {
               Statement stmt = conn.createStatement();
               ResultSet namesList = stmt.executeQuery("SELECT name FROM Users");
               try
               {

                   while (namesList.next())
                   {
                       rowNames.add(namesList.getString(1));

                       contactListNames =(String[])rowNames.toArray(new String[rowNames.size()]);

                       // this used to print out contents of array list
                      // System.out.println("" + rowNames);

                       while(i<contactListNames.length)
                       {
                           System.out.println(" " + contactListNames[i]);
                           i++;
                       }
                   }
               }
               catch(SQLException q)
               {
                   q.printStackTrace();
               }
               conn.commit();
               stmt.close();
               conn.close();
           }
           catch(SQLException e)
           {
               e.printStackTrace();
           }
       }

i really need help here. im at my wits end. i just cant see why the first method would add to the JList no problem. but the second one wont.

both the contactListNames array and array list can print fine and have the names in them. but i must be transfering them too the jlist wrong. please help

p.s im aware this is long. but trust me its the short version.

© Stack Overflow or respective owner

Related posts about java

Related posts about sql