1

okay, here is the problem, I am writing a simple chat program. a client has a JFrame called the ChatList class that contains a JList which shows online users. The list is refreshed with new status of users every 2 seconds using this method:

public void refresh(ArrayList<String> onlineUsers) {
   System.out.println(onlineUsers);
    DefaultListModel listModel2 = new DefaultListModel();
    for (int i = 0; i < onlineUsers.size(); i++) {
        if (!onlineUsers.get(i).equals(client.userName))
            listModel2.addElement(onlineUsers.get(i));
    }
    if (!listModel2.equals(listModel)) {
        listModel=listModel2;
        listbox.setListData(listModel.toArray());
        listbox.validate();
    }
        topPanel.add(listbox, BorderLayout.CENTER);
        this.setVisible(true);    
    }

if the user chooses a client to chat to, another JFrame opens. The problem is that this JFrame wont hold the focus and the focus alternatively swings between the ChatList and the ChatWindow, which causes the user not to have enough time to even write a simple "Hello". so now I have 2 questions:

  • is it because of updating my jlist?
  • is there a way to stop this and make the ChatList only get the focus when i click on it and not by updating it?
Peggy
  • 394
  • 6
  • 22
  • did you tried with any of these ? Your_frame.toFront(); Your_frame.toBack(); Your_frame.setEnabled(False); – Arijit May 27 '14 at 17:29
  • @Arijit yeah i tried the latter, that didn't work, i also called requestFocus() for the other frame but that didn't work either :( – Peggy May 27 '14 at 17:54
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 28 '14 at 00:15

1 Answers1

1

If you want to change the users in the ChatList, then all you need to do is update the ListModel. The model will then notify the JList that the data have changed and the JList will repaint itself, but is should NOT take focus away from the currently active window.

I don't think any of the following code is necessary:

listbox.validate();

topPanel.add(listbox, BorderLayout.CENTER);
this.setVisible(true);    
camickr
  • 321,443
  • 19
  • 166
  • 288
  • `listbox.validate();` is needed but you were right the problem was `this.setVisible(true);` – Peggy May 27 '14 at 18:15
  • @Peggy, No the validate() is not needed. The ListModel is responsible for notifying the JList when the model is changed and the JList will repaint itself. If you think you need that method then you still have a problem with your program somewhere. – camickr May 28 '14 at 01:37
  • @making the same statement will not fix the problem or give us any more information. All updates should be done to the model and the code must be executed on the Event Dispatch Thread. Then the JList will repaint itself. You do not need to create a new JList or add the JList to the GUI since it is already added to the GUI. Post a `SSCCE` that demonstrates the problem. – camickr May 28 '14 at 14:56