0

Why isn't my JList updating?

I re-instantiate the list object. I repaint and revalidate... What's going wrong?

needmoredetailsneedmore

External class:

                ControlPanel.usernames.add(username);
            ControlPanel.list = new JList(ControlPanel.usernames.toArray());
            ControlPanel.panel.repaint();
            ControlPanel.panel.revalidate();

ControlPanel.java

package main;

import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

@SuppressWarnings("serial")
public class ControlPanel extends JFrame {

public static JPanel panel = new JPanel();
JScrollPane scrollpane;
static JLabel players;
static String s;
public static ArrayList<String> usernames = new ArrayList<String>();
public static JList list;

// public static String categories[] = {};

public ControlPanel() {
    list = new JList(usernames.toArray());
    scrollpane = new JScrollPane(list);

    panel.add(scrollpane, BorderLayout.CENTER);
    add(panel);

    setTitle("RuneShadows CP");
    setSize(400, 400);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3130731
  • 655
  • 2
  • 9
  • 12
  • [This answer](http://stackoverflow.com/a/4262716/877472) I believe shows how you can update the JList using the list's model object (rather than creating an entirely new JList). – Paul Richter Dec 24 '13 at 00:26
  • Can't you please be a little more nice to us and post a [SSCCE](http://sscce.org). Though it is apparent that Mad programmer's answer is right as given below – Sage Dec 24 '13 at 00:30

1 Answers1

4

You've created a new JList which has not been added to the screen

Instead, create a new ListModel and set it as the model for the list that's already on the screen

ControlPanel.usernames.add(username);
ControlPanel.list,setModel(new UserNamesListModel(ControlPanel.usernames));
ControlPanel.panel.repaint();
ControlPanel.panel.revalidate();

See How to use lists for more details

Your access model is, frankly, scaring. It allows complete access to everything form anywhere, so any nasty class might come along and completely dismantle your UI, for example.

The use if static is not a solution for easy access and greatly reduces the flexibility of your application over time.

You should be providing simply getters and setters to allow outside classes access to the information contained within and managed by the class.eapqually, you should provide the class's with management functionality that other class might need to use, such as refresh.

No one should care exactly how refresh works, only that it does...

Start by taking a look at How to use lists

import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;

public class UserNamesListModel extends AbstractListModel<String> {

    private List<String> userNames;

    public UserNamesListModel(List<String> userNames) {
        this.userNames = new ArrayList<>(userNames);
    }

    @Override
    public int getSize() {
        return userNames.size();
    }

    @Override
    public String getElementAt(int index) {
        return userNames.get(index);
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I'm sorry but I'm not familliar with ListModel, can you give me an example? I have ListModel lm = new DefaultListModel(); with JList list = new JList(lm) – user3130731 Dec 24 '13 at 00:32
  • I can't figure out what I replace UserNamesListModel with. I tried ArrayList and JList. – user3130731 Dec 24 '13 at 00:42