Fetch array of strings from ArrayList for JList Java

373 views Asked by At

the task which I do is quite simple, but I faced one problem with JList component.

What i need is. I fetch data from DataBase, load it to String array, pack array into ArrayList (just because I duno how many records I have), return it from method as ArrayList. Now at receiver side. I have a trouble, I cant fetch String array from ArrayList so that I can pass to JList.

Here is the code.

ArrayList<Object> fetch = new ArrayList<Object>();
public String[] data =new String[10];
    listModel = new DefaultListModel();      
    myList = new JList(listModel);
    //myList = new JList(data);
    // So it works with simple array of strings.
    fetch=DAO.loadPasswords();
     //At this point it asks me to cast an object to smth which late cause null pointer exception.
    myList = new JList(fetch.get(0));

And here is loadPasswords();

public static ArrayList<Object> loadPasswords(){
    dbConnect();
    boolean unswer = false;
    String[] result=null;
    String query1 = "select * from tblPasswordsStorage where "
            + "_user_id = ?";
    String userId=Personal_Organizer.userProfile.getUserID();
    ArrayList<Object> params = new ArrayList<Object>();
    params.add(userId);
    executeQueryP(query1, params);
    ArrayList<Object> fetched=null;
    try {
        if (rs.next()) {
            unswer = true;
            //Personal_Organizer.userProfile.setUserID(rs.getString(1));
            result[0]=rs.getString(1);
            result[1]=rs.getString(2);
            result[2]=rs.getString(3);
            result[3]=rs.getString(4);
            result[4]=rs.getString(5);
            result[5]=rs.getString(6);
            result[6]=rs.getString(7);
            result[7]=rs.getString(8);
            result[8]=rs.getString(9);
            result[9]=rs.getString(10);
            fetched.add(result);


        }
        if (unswer) {
            while (rs.next()) {
                Tools.print(rs.getString(1));
            }
        }
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null,
                "SQL Server connection issue.\n"
                + "Please, check Server address, user name and password.",
                "Output",
                JOptionPane.PLAIN_MESSAGE);
    }
    dbClose();
    return fetched;
}

I tried to use multidimensional array but still JList require single dimensional array.

1

There are 1 answers

1
Hovercraft Full Of Eels On

Either use a DefaultListModel and load it with your Strings using a simple for or for-each loop that loops through your ArrayList, adding each String item in the ArrayList into the model, or create your own ListModel class, one that extends AbstractListModel and that uses the ArrayList as its data nucleus.

Edit, since your data is held in an ArrayList of arrays, then perhaps you don't want to display it within an ArrayList after all, but rather in a JTable. Here your best bet would be to create your own TableModel, one based off of AbstractTableModel and that uses your ArrayList as its data nucleus. Also, perhaps better than loading your data into an array would be to create a custom class to hold each row of data from the ResultSet.


You state:

My problem is to simply load something to JList. The idea is to display application/website name in the list, then when you click on it, you get full data which contains in array. That's why I put fetch.get(0)...because. I need at least something.

Then

  • Create a custom class to hold each row of data.
  • Create items of this class from each row of the ResultSet
  • Fill your DefaultListModel with items of this class
  • Give your JList a custom ListCellRenderer, one that display's the application/website name
  • Then you can display the name and still have each list item hold all the pertinent information that it needs.