I'm working on a college project, it involves selecting a user from a list and "logging in". I currently get the data from the database and use a while loop to add the data to the JComboBox:
while(rs.next())
{
userID = rs.getInt("user_ID");
fname = rs.getString("fname");
lname = rs.getString("lname");
userSelect.addItem(userID + " " + fname + " " + lname);
}
I then want to pass the userID of the selected item to the new JFrame and just the userID. From there I can get the rest of the information of the user. What would be the best way to approach it? To splice the string?
I have a submit button that runs the following
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == select) {
Object item = userSelect.getSelectedItem();
System.out.println(item);
UserDash getDash = new UserDash(item);
this.dispose();
}
}
just an update to my question from yesterday. I used the following to be able to pass data from a JComboBox to another JFrame
public SelectUser() {
// Database Connection & Query Code Here
while(rs.next())
{
Integer userID = rs.getInt("user_ID");
String fname = rs.getString("fname");
String lname = rs.getString("lname");
item = new Object[] {userID, fname, lname};
userSelect.addItem(new Item(userID, fname, lname));
}
private class Item {
private int userID;
private String fname;
private String lname;
public Item(Integer userID, String fname, String lname) {
this.userID = userID;
this.fname = fname;
this.lname = lname;
}
public Integer getUserID() {
return userID;
}
public String getFName() {
return fname;
}
public String getLName() {
return lname;
}
@Override
public String toString() {
return "ID: " + getUserID() + " " + getFName() + " " + getLName();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == select) {
Item item = (Item) userSelect.getSelectedItem();
System.out.println(item.getUserID() + " " + item.getFName() + " " + item.getLName());
UserDash runDash = new UserDash(item.getUserID(), item.getFName(), item.getLName());
}
}
}
I hope this helps anyone looking to do the same or something similar!
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.