-2

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.

My 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.

Jurdun
  • 147
  • 2
  • 14
  • @MadProgrammer My emphasis is on netbeans. – Jurdun Jan 07 '18 at 01:05
  • 3
    Irrelevant, comparing `String` values in Java is a fundamental concept, one which you are doing wrong – MadProgrammer Jan 07 '18 at 01:06
  • I've used .equals () , wasn't sure if there's any difference in where and how i should use it as opposed to ==. I tried it again just then and get an error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException – Jurdun Jan 07 '18 at 01:10
  • "however honestly i dont think a list is necessary". Can you explain why? – Shankha057 Jan 07 '18 at 01:20
  • Additional gui fluff. I've been trying before i attempted to do it this way for it to check whether user1, user2, user3, etc has not been given any data and if it has not then thats the object the users input data will be added to. However with these methods it has seen that all of them are empty and has added the same user data to all of them and then cannot be attempted by the end user again. The reason is said that was that i believe that there must be a way to check if user1, user2 etc is empty automatically without the end user selecting what object they which to change the name/type of. – Jurdun Jan 07 '18 at 01:24

1 Answers1

1

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);
        }

    }
Daniel
  • 447
  • 5
  • 22