my problems is that i dont know how to make a list (Jlist from NetBeans) with some data from my derby db
EX:
I have a form that saves the id,name,phone and email from a "client", and i want to show all the names from my previously registered clients in a list, the problem is that i need to convert the varchar data into a listmodel. How can i do that? I tried this, but didn't work:
while (rs.next()){
int id_col = rs.getInt("clientID");
String name = rs.getString("clientName");
client_listClients.setText(name);
}
It says that "couldn't find symbol" for .setText
EDIT using DefaultListModel
WORKING RESULT!
public void DoConnect( ) {
DefaultListModel model = new DefaultListModel();
client_listClients.setModel(model);
try {
String host = "jdbc:derby://localhost:1527/ANAIA_DB";
String uName = "*****";
String uPass = "*****";
con = DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement();
String sql = "SELECT * FROM APP.CLIENT";
rs = stmt.executeQuery(sql);
while (rs.next()){
int id_col = rs.getInt("clientID");
String name = rs.getString("clientName");
model.addElement(name);
}
} catch (SQLException err) {
JOptionPane.showMessageDialog(main.this, err.getMessage());
}
}
Thx aloooot @peeskillet
"It says that "couldant find symbol" for .setText"
JList
has no method setText()
. When in doubt, read the docs
Instead, you'll want to use a ListModel
. If you don't want to create your own, you can use the provided DefaultListModel
implementation. There you have the method addElement
.
You first need to declare your your list with the model.
DefaultListModel model = new DefaultListModel();
JList client_listClients = new JList(model);
Then you can use the method addElement
, which will automatically update the UI after all additions are made. No need to do anything with the list.
while (rs.next()){
String name = rs.getString("clientName");
model.addElement(name);
}
You can see more at How to use Lists , and focus on the section Creating a Model
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.