setListData merging array items together

Google has been very unhelpful, and believe me this is a last very sad resort. Or don’t I don’t care.

I create a JList, and when i call setListData(String[]) it merges all the items in my String[] into one and sticks it in one slot of the List.

Here is how I create it. rooms = {“Loading List”}

DefaultListModel listModel = new DefaultListModel();
        list = new JList(listModel);
        list.setListData(rooms);

Here is where I try and use setListData

public void setRooms(String[] rooms) {
        int index = list.getSelectedIndex();
        list.setListData(rooms);
        list.setSelectedIndex(index);
    }

In the specific case I have been trying rooms is {“Frog”, “Toad”}

The JList’s first item changes from “Loading List” to “FrogToad”

I also tried converting the String[] to a Vector and adding it that way, same result.

Neat…

Here is all my code, I changed how I did it to every imaginable way including making my own subclass of AbstractListModel, and get the same results every time.

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

public class Welcome extends JFrame implements ActionListener {

    private String[] rooms;
    
    private static JButton quit = new JButton("Quit");
    private static JButton join = new JButton("Join");
    private static JButton make = new JButton("Make");
    
    private DefaultListModel listModel;
    private static JList list;

    private SocketAction host;
    
    public Welcome(String[] rooms, SocketAction host) {
        super("Connect 4");
        quit.addActionListener(this);
        make.addActionListener(this);
        join.addActionListener(this);
        this.host = host;
        this.rooms = rooms;
        
        listModel = new DefaultListModel();
        listModel.addElement(rooms[0]);
        list = new JList(listModel);
        
        JPanel content = new JPanel(new BorderLayout());
        content.add(new JLabel("Join or make a game."), BorderLayout.NORTH);
        content.add(new JScrollPane(list), BorderLayout.CENTER);
        
        JPanel southPanel = new JPanel();
        southPanel.add(join);
        southPanel.add(make);
        southPanel.add(quit);
        
        content.add(southPanel, BorderLayout.SOUTH);
        add(content);
        setLocation(400, 400);
        setResizable(false);
        pack();
        setVisible(true);
    }
    
    public void setRooms(String[] rooms) {
        listModel.clear();
        list.setVisibleRowCount(rooms.length);
        int i = 0;
        for (String s : rooms) {
            listModel.add(0, s);
            list.setSelectedIndex(i);
            list.ensureIndexIsVisible(i);
            i++;
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == quit) {
            host.send("quit");
            System.exit(0);
        }
        else if (e.getSource() == join) {
            String name = (String)list.getSelectedValue(); 
            if (!name.equals("Loading List") && name != null && !name.equals("null")) {
                host.send("join" + name);
                setVisible(false);
            }
        }
        else if (e.getSource() == make) {
            String name = JOptionPane.showInputDialog("Game Name");
            if (name != null) {
                host.send("make" + name);
                setVisible(false);
            }
        }
    }
}

Works for me… even if I change setRooms() to


    public void setRooms(String[] rooms) {
        list.setListData(rooms);
    }

:o