JList not Displayable

244
December 14, 2017, at 09:24 AM

I have been working on this problem for a couple of days and I cannot understand where I went wrong. I have created a Server-Client Chat Program and on the Server GUI there is a tab that shows a list of users. This list works in every way that I want. I wanted to add a UserList to the Client GUI and the JList is there, but when I update the DefaultListModel, the JList only updates on the ServerGUI. I tried to debug and found that the JList on the ChatGUI is not displayable and I don't know why, or how to fix it.

Here is my (relevant) code:

Client Class

public class Client {
    String username;
    Socket socket;
    PrintWriter out;
    Scanner in;
    public Client (String username, Socket socket, PrintWriter out, Scanner in) {
        this.username = username;
        this.socket = socket;
        this.out = out;
        this.in = in;
    }
}

ServerGUI Class - (the register method is called later in the program when a Client joins)

public class ServerGUI {
    public volatile static ArrayList<Client> users;
    public static DefaultListModel<String> model = new DefaultListModel<String>();
    static Client clientReg;
    public static void register(Client client) {
        clientReg = client;
        users.add(clientReg);
        model.addElement(clientReg.username);
         ServerView.userList.setModel(model);
        ChatView.userList.setModel(model);
    }
}

ChatView Class

public class ChatView extends JFrame {
    public JPanel contentPane;
    public static JList<String> userList = new JList<String>();
    public static JTextArea chatOutput;
    private JTextField inputField;
    public ChatView() {
        setResizable(false);
        setTitle("Chat GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 550, 475);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
        tabs.setBounds(0, 0, 535, 435);
        contentPane.add(tabs);
        JPanel chatViewer = new JPanel();
        tabs.addTab("Chat", null, chatViewer, null);
        chatViewer.setLayout(null);
        // Code that makes up the chatViewer JPanel
        JPanel userListPane = new JPanel();
        tabs.addTab("User List", null, userListPane, null);
        userListPane.setLayout(null);
        JLabel label = new JLabel("User List:");
        label.setBounds(10, 10, 510, 20);
        userListPane.add(label);
        userList.setModel(new AbstractListModel<String>() {
            public String getElementAt(int index) {
                return ServerGUI.model.get(index);
            }
            public int getSize() {
                return ServerGUI.model.size();
            }
        });
        userList.setValueIsAdjusting(true);
        userList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        userList.setBounds(10, 40, 510, 395);
        userListPane.add(userList);
    }
}

Most of my programming has been self taught so if there is any format that is incorrect please let me know so that I may be able to correct it.

Answer 1

OK, I see how you're trying to "share" models via static fields and methods:

userList.setModel(new AbstractListModel<String>() {
    public String getElementAt(int index) {
        return ServerGUI.model.get(index);
    }
    public int getSize() {
        return ServerGUI.model.size();
    }
});

and this will never work, and is not how clients and servers transfer information. For one, while yes you have the server static fields and methods available to the client, the actual server class and instance are running in a completely different JVM, usually on a different machine entirely, and so the data that you're getting does not reflect the true state of the actual Server object and static class state on the server itself, but is a mere shadow of the correct object/class.

Suggestions:

  • Use a standard list model for your client, and delete the code you're using, it won't work
  • If this is true client/server code where you transmit information over a socket, then the client must get updates from the server through the socket. We have no idea how you're wiring this so, I cannot give specific recommendations other then -- get the data from the socket
  • And do so in a background thread -- i.e., the client should receive socket data off of the Swing event thread
  • But then use this information received to update the client's list model on the event thread. One way to do this is via a SwingWorker<Void, String> using its process / publish method pair.
Rent Charter Buses Company
READ ALSO
Java MouseListener Delaying MouseEvents For Duration, Then Hearing Them All At Once

Java MouseListener Delaying MouseEvents For Duration, Then Hearing Them All At Once

I am a student working on a personal project of a virtual cardgame styled after hearthstone or mtg if you are familiarThe way it works is this: you have a "hand" of cards on the right side of the screen and you can click or drag them to 'play' them onto the board

194
Java Maven - Loading native library

Java Maven - Loading native library

I'm currently trying to use JNI for my project, and I've already created the library using maven aswellNow I just can't find any way to load the library that is not simply executing the java program from the command line

305
Why might changing a field from int to Integer cause objects to disappear from Sets?

Why might changing a field from int to Integer cause objects to disappear from Sets?

There's a presentation on AutoValue (a terse way to define immutable objects in Java, with sensible defaults for equals, hashCode, etc): https://docs

266
Using Swing Library to Move Buttons

Using Swing Library to Move Buttons

I am trying to create a type of Graphics Editor that allows users to create graphic depictions of American Football playsTo do this, the User should be able to do the following:

278