0

I don't know what I am doing wrong. I am trying to take a JTextField user input to be stored and displayed in a JList, but every time the button is pressed to store the user input the JList remains blank. Any help would be greatly appreciated.

DefaultListModel<String> model = new DefaultListModel<String>();
        menuList = new JList<String>(model);
        menuList.setBounds(500, 65, 300, 400);
        menuList.setSelectionBackground(Color.LIGHT_GRAY);
        menuList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        btnCreateMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                childFrame = new JFrame("New Menu");
                childFrame.setBounds(340, 300, 400, 200);
                childFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                childFrame.getContentPane().setLayout(null);
                childFrame.setVisible(true);

                lblNewMenu = new JLabel("Menu Name:");
                lblNewMenu.setBounds(30, 60, 200, 20);
                childFrame.getContentPane().add(lblNewMenu);

                input = new JTextField();
                String userInput = input.getText();
                input.setBounds(lblNewMenu.getX() + 80, lblNewMenu.getY(), 250, 30);
                childFrame.getContentPane().add(input);


                btnMenuInput = new JButton("Create New Menu");
                btnMenuInput.setBounds(120, 100, 200, 30);
                btnMenuInput.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){ 

                        model.addElement(userInput);
                        menuList.setModel(model);
                        childFrame.setVisible(false);

                        Entree selectedEntree = (Entree)cboEntrees.getSelectedItem();
                        Side selectedSide = (Side)cboSides.getSelectedItem();
                        Salad selectedSalad = (Salad)cboSalads.getSelectedItem();
                        Dessert selectedDessert = (Dessert)cboDesserts.getSelectedItem();


                        Menu menu = new Menu(userInput, selectedEntree, selectedSide, selectedSalad, selectedDessert);
                        menuArray.add(menu);
                    }
                });
                childFrame.getContentPane().add(btnMenuInput);  
            }
        });

        mainframe.setVisible(true);
mdh75
  • 25
  • 2
  • 7
  • 1
    Read the section from the Swing tutorial on [How to Use Lists](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html) for a working example that does exactly what you want. Also, don't use setBounds(). Swing was designed to be used with layout managers. The examples from the tutorial use layout managers. – camickr Nov 30 '17 at 19:52

2 Answers2

1

This line

userInput = input.getText();

needs to be called first in the ActionListener. Otherwise you never get the latest String from the text field.

e.g.,

public void actionPerformed(ActionEvent e){ 
    userInput = input.getText();
    model.addElement(userInput);
    //menuList.setModel(model); // not needed

Also, as mentioned by camickr in comment, avoid using null layouts and setBounds as this fights against the Swing GUI library rather than working with it, making it much harder to create flexible easy to update and edit GUI's.

Also, the childFrame top-level window should be a JDialog and not a second JFrame. Please see The Use of Multiple JFrames: Good or Bad Practice? for more on this.

0

Also note that you should create the entire dialog or Frame before you make it visible, or else some of the items may not be visible at first.

Another problem is the use of JFrame.HIDE_ON_CLOSE. You should probably be using DISPOSE_ON_CLOSE instead. Otherwise the frame will just be hidden, but will still exist, possibly for the life of the program.

FredK
  • 4,094
  • 1
  • 9
  • 11