[英]How to pass selected item data from JComboBox to another JFrame class?
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 循环将数据添加到 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.然后我想将所选项目的用户 ID 传递给新的 JFrame 和用户 ID。 From there I can get the rest of the information of the user.从那里我可以得到用户信息的rest。 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我使用以下能够将数据从 JComboBox 传递到另一个 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!我希望这可以帮助任何想要做同样或类似事情的人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.