Issue
I'm trying to create multiple objects from user input from a Jframe form. I've seen many posts on here on how to do this with the console, which all the answers end up being put in a loop; however none asking how to do this from a Jframe form.
private void CreateAccount() {
String selected = UserSelectList.getSelectedValue();
String setName = nameTboxSet.getText();
String setType = UserTypeBoxSet.toString();
if ("user1" == selected) {user1.setName(setName); user1.setUserType(setType);}
else if ("user2" == selected) {user2.setName(setName); user2.setUserType(setType);}
else if ("user3" == selected) {user3.setName(setName); user3.setUserType(setType);}
else if ("user4" == selected) {user4.setName(setName); user4.setUserType(setType);}
else if ("user5" == selected) {user5.setName(setName); user5.setUserType(setType);}
else if ("user6" == selected) {user6.setName(setName); user6.setUserType(setType);}
else if ("user7" == selected) {user7.setName(setName); user7.setUserType(setType);}
else if ("user8" == selected) {user8.setName(setName); user8.setUserType(setType);}
else if ("user9" == selected) {user9.setName(setName); user9.setUserType(setType);}
else if ("user10" == selected) {user10.setName(setName); user10.setUserType(setType);}
}
Above is what I have currently come up with, however honestly i dont think a list is necessary, i've tried many things to try and figure out how to get this work.
Solution
In your code you have incorrectly determined which item has been selected. Assuming you used the java.awt.List component, this is how you would achieve what you have tried to do.
if (listName.getSelectedItem.equals(“user1”)){ ... }
For more information on this, please visit the Oracle documentation on the List component: https://docs.oracle.com/javase/7/docs/api/java/awt/List.html
I have coded a quick example to show you it in action:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class TestFrame extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
List list = new List();
list.add("user1", 0);
list.add("user2", 1);
list.add("user3", 2);
list.add("user4", 3);
list.add("user5", 4);
list.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(list.getSelectedItem().equals("user1")){
System.out.println("User 1 has been selected!");
}else if(list.getSelectedItem().equals("user2")){
System.out.println("User 2 has been selected!");
}
}
});
contentPane.add(list);
setContentPane(contentPane);
}
}
Answered By - Daniel