0

有没有人有任何好的教程来用这个查询中的 sql 表填充 JList。每次创建或删除表时,JList 都会更新。

SELECT name FROM sqlite_master WHERE type = 'table';

任何帮助,将不胜感激。

4

2 回答 2

1

This is simple, you do not need a tutorial :) Build your JList from the resuting list of elements using this costructor.

Iterate on the query result cursor and fill a Vector with the elements you got from the DB. Then use the Vector as a the underlying data container (model) for building you JList:

Vector<String> elements = new Vector<String>();
while (query.next()) {
    // or whatever is appropriate
    elements.add(query.getString("name"));
}  
Jlist mylist = new Jlist(elements);

You should split the DB query code (model related) from the GUI building (view) to ease future maintainance and decouple the structure in a so-called MVC structure, but this is out of the scope of this question.

于 2013-05-09T13:57:33.173 回答
1

You can use DefaultListModel see this example :

    //......
    DefaultListModel listModel = new DefaultListModel();
    String query = "SELECT name FROM sqlite_master WHERE type = 'table'";
    rs = st.executeQuery(sql);
    while(rs.next){
    listModel.addElement(rs.getObject("name"));// I think you want get this field
    }

    list.setModel(listModel);
于 2013-05-09T13:58:18.420 回答