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();                
        nameList = new JList(names);

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

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


Recommended Answers

All 4 Replies

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() {
        JPanel panel = new JPanel();
        panel.add(new JButton(new AbstractAction("Add") {

            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (count <= N) {
        panel.add(new JButton(new AbstractAction("Remove") {

            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                int itemNo = list.getSelectedIndex();
                if (itemNo > -1) {
                    removeActionPerformed(e, itemNo);
        for (int i = 0; i < N - 2; i++) {
        list.setVisibleRowCount(N - 2);
        dlg.add(sp, BorderLayout.CENTER);
        dlg.add(panel, BorderLayout.SOUTH);

    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) {

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

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

            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, learning, and sharing knowledge.