|
-
April 30th, 2010, 08:11 AM
#1
help with jList and arraylist
Hello! I'm pretty new to java and programming and general and could need some help with how to solve the problem I'm having.
Here are the two classes I have that covers this part:
public class deltagarinmat {
dbconnection dbcon = new dbconnection();
ResultSet rs = dbcon.getData("SELECT * FROM deltagare");
public List<deltagare> getAllPeople() {
List<deltagare> data = null;
try {
data = new ArrayList<deltagare>();
while (rs.next()) {
deltagare delt = new deltagare();
int nr = rs.getInt(1);
String name = rs.getString(2);
String username = rs.getString(3);
String password = rs.getString(4);
delt.setPersNR(nr);
delt.setNamn(name);
delt.setLogin(username);
delt.setPass(password);
data.add(delt);
}
} catch (SQLException e) {
} finally {
return data;
}
}
}
AND
public class deltagare {
private int persNR;
private String namn;
private String login;
private String pass;
public deltagare() {
}
public deltagare(int nr, String name, String username, String password) {
setPersNR(nr);
setNamn(name);
setLogin(username);
setPass(password);
}
public int persNR() {
return persNR;
}
public void setPersNR(int persNR) {
this.persNR = persNR;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getNamn() {
return namn;
}
public void setNamn(String namn) {
this.namn = namn;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
These are the two classes I have to first get the data from my database into a arraylist. And then a simple class with set and get. I would like to make a jList with the data I got in my arraylist. But I don't really know how to do it.
-
April 30th, 2010, 08:33 AM
#2
Re: help with jList and arraylist
To add items to a JList when you don't have a Vector or an array of items to pass to the constructor, you need to use the ListModel, which is where the data gets stored for a JList. The one to use is DefaultListModel, which has various add, remove, and get methods for the items.
So create a DefaultListModel, add the items to the DefaultListModel (use a loop to iterate over the ArrayList). Then you can create the JList, passing the DefaultListModel to its constructor. If you have already created the JList, set the DefaultListModel into it using the setModel method.
Alternatively, you can convert the ArrayList to an array and pass that to the JList constructor. The ArrayList.toArray() method will return an array of the items.
Any programming problem can be solved by adding a level of indirection...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|