Convert ArrayList to DefaultListModel
Asked Answered
P

2

6

I'm beginner in Java. I really need to return DefaultTableModel (javax.swing) from array or ArrayList. It is possible? I can't insert array into DefaultTableModel (constructor).

Code is below:

private DefaultListModel model;


public DefaultListModel getNamesAndIdToCombobox(Connection conn, boolean closeConn, String sql) throws SQLException {

    long counter = 0;

    try {
        Statement stmt = 
                conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery(sql);

        while (rs.next()) {
            // String longKey = (String)rs.getString(2);
            try
            {
                jListList.add(new JListValues(rs.getLong(2), rs.getString(1)));
            }
            catch(SQLException sqlException){}

            try
            {
                jListList.add(new JListValues(rs.getLong(2), rs.getLong(1)));
            }
            catch(SQLException sqlException){}

            try
            {
                jListList.add(new JListValues(rs.getString(2), rs.getLong(1)));
            }
            catch(SQLException sqlException){}
            counter++;

        }
        JListValues[] array = jListList.toArray(new JListValues[jListList.size()]);


        model = new DefaultListModel(array);       // HERE IT IS A PROBLEM

        LOGGER.info("getNamesAndIdToCombobox result count: " + counter);
    } catch (SQLException e) {
        LOGGER.error("Error", e);
        throw e;
    } finally {
        try {
            if (closeConn == true)
                conn.close();
        } catch (Exception e) {/* null */
        }
    }
    return model;
}
Physiology answered 2/10, 2013 at 19:6 Comment(2)
what is the problem exactly?Promethium
So create a loop and copy the values one by one to a Vector and then create the DefaultListModel using the Vector. Or, just load the values into a Vector so you don't need to do the copy. Also, clear up you question. Sometimes you refer to a TableModel and other times you refer to a ListModel.Burnisher
O
4

adding the following code for adding arraylist values to DefaultListModel should work:

 DefaultListModel<JListValues> model = new DefaultListModel<>()
 for(JListValues val : array)
         model.addElement(val);
Oat answered 2/10, 2013 at 19:25 Comment(1)
put JListValues inside the diamond<> of new DefaultListModel<>(), that is:DefaultListModel<JListValues> model = new DefaultListModel<JListValues>()Oat
B
5

with the following, there is no need to iterate through a data set and is much more efficient.

JList<String> jlist = new JList<String>(new String[]{"a","b","c","d"});

DefaultListModel<String> defaultListModel = (DefaultListModel<String>)jlist.getModel();

ArrayList<String> arrayList = Collections.list(defaultListModel.elements());
Byrnie answered 9/9, 2014 at 20:40 Comment(1)
It's doing the reverse of what the OP is asking, btwAmmadas
O
4

adding the following code for adding arraylist values to DefaultListModel should work:

 DefaultListModel<JListValues> model = new DefaultListModel<>()
 for(JListValues val : array)
         model.addElement(val);
Oat answered 2/10, 2013 at 19:25 Comment(1)
put JListValues inside the diamond<> of new DefaultListModel<>(), that is:DefaultListModel<JListValues> model = new DefaultListModel<JListValues>()Oat

© 2022 - 2024 — McMap. All rights reserved.