I'm having trouble figuring this out. I have 3 jlists and each one has a textbox and a "add" button so the user can enter text to be added to the list. I have read probably a dozen webpages or forum posts on this and all have pretty much the same code advice for doing this. The problem I'm having is that none have said or shown how the code actually links the button to the jlist1, jlist2, and jlist3 respectively. What am I missing?? Here is the code I have and not sure what to change or add.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        String addName = (jTextField1.getText());        
        names = new DefaultListModel();                
        names.addElement(addName);
        nameList = new JList(names);
        jTextField1.setText("");
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        String addPlace = (jTextField2.getText());
        places = new DefaultListModel();                
        places.addElement(addPlace);
        placeList = new JList(places);
        jTextField2.setText("");
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        String addDay = (jTextField3.getText());
        days = new DefaultListModel();                
        days.addElement(addDay);
        dayList = new JList(days);
        jTextField3.setText("");
    }


Thanks

better once time to see that as 100 times ....

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

// based on @trashgod code
/** @see http://stackoverflow.com/questions/5759131 */
// http://stackoverflow.com/questions/8667719/jdialog-repaint-after-jlist-modification

public class ListDialog {

    private static final int N = 12;
    private JDialog dlg = new JDialog();
    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);
    private JScrollPane sp = new JScrollPane(list);
    private int count;

    public ListDialog() {
        list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        JPanel panel = new JPanel();
        panel.add(new JButton(new AbstractAction("Add") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                append();
                if (count <= N) {
                    list.setVisibleRowCount(count);
                    dlg.pack();
                }
            }
        }));
        panel.add(new JButton(new AbstractAction("Remove") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                int itemNo = list.getSelectedIndex();
                if (itemNo > -1) {
                    removeActionPerformed(e, itemNo);
                }
            }
        }));
        for (int i = 0; i < N - 2; i++) {
            this.append();
        }
        list.setVisibleRowCount(N - 2);
        dlg.add(sp, BorderLayout.CENTER);
        dlg.add(panel, BorderLayout.SOUTH);
        dlg.pack();
        dlg.setLocationRelativeTo(null);
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.setVisible(true);
    }

    private void removeActionPerformed(ActionEvent e, int itemNo) {
        System.out.println("made_list's model: " + list.getModel());
        System.out.println("Model from a fresh JList: " + new JList().getModel());
        model = (DefaultListModel) list.getModel();
        if (model.size() > 0) {
            if (itemNo > -1) {
                model.remove(itemNo);
            }
        }
    }

    private void append() {
        model.addElement("String " + String.valueOf(++count));
        list.ensureIndexIsVisible(count - 1);
    }

    public static void main(String[] a_args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ListDialog pd = new ListDialog();
            }
        });
    }
}

Thanks for the reply mKorbel. Not to seem unappreciative but with all the sources I've seen giving instruction on how to ADD being similar to what I was trying to use (granted it wasn't working), I thought it would be much simpler. If you couldn't tell, I'm just a beginner at java and the gui and half of what you coded is quite foreign to me. Additionally, as to my original question, what part of the code is instructing which of the 3 jlist's to perform the action on. I guess I'm saying, if my container only had one jlist than I could see that maybe that would work without any program confusion but with 3 lists in the same container, how does the program/jbutton know which jlist I want to add an item to? In your code above, which part or variable should be pointing to the specific jlist I want to effect? Should each jlist and set of controls be in it's own container? My inexperience may be part of the problem here but surely the list being modified will need to be identified, right?

there is simple answer you recreated JList, its XxxModel, and this JList isn't added to the Container, then you never can see any changes

Well thanks anyway but your answers are just to vague and nonspecific to help me. I still don't know if each jlist must be in it's own container or how the newly created JList is tied to the corrosponding jlist in my jform or what exactly is wrong or missing from my code which several respectable online tutorials state is how to accomplish what I'm trying to do, I mean it must be close I would think. I was kinda hoping someone would be able to point out the specific problem and show me how to fix it but it seems to be getting way complicated and I really am not any closer to understanding why it doesn't work and what specifically should be changed or added. Maybe I'm just bad at explaining what I need or another explanation would make more sense to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.