9

I have JComboBox based on ArrayList:

private ArrayList<String> klienci = new ArrayList<String>();
private JComboBox klienciLista;

and I add it in constructor:

klienciLista = new JComboBox(klienci.toArray());
klienciLista.setPrototypeDisplayValue("#############################");
panel.add(klienciLista); //JPanel panel

At the start List is empty. Client gets via socket new ArrayList in thread:

public void run() {
  try {
   host = InetAddress.getLocalHost().getHostName();
   socket = new Socket(host, SERVER_PORT);
   input = new ObjectInputStream(socket.getInputStream());
   output = new ObjectOutputStream(socket.getOutputStream());
   output.writeObject(nazwa);
  } catch (IOException e) {
   System.out.println(e);
   JOptionPane.showMessageDialog(null,
     "Polaczenie sieciowe dla klienta nie moze byc utworzone");
   setVisible(false);
   dispose(); // zwolnienie zasobów graficznych
      // okno graficzne nie zostanie utworzone
   return;
  }
  try {
   while (true) {
    container = new Object[2];
    container = (Object[]) input.readObject();
    String m = (String) container[0];
    setKlienci((ArrayList<String>) container[1]);
    klienciLista = new JComboBox(klienci.toArray());
    String pom = textArea.getText();
    textArea.setText(pom + ">>> " + m + "\n");
    klienciLista.revalidate();
    panel.revalidate();
    panel.repaint();

    if (m.equals("exit")) {
     input.close();
     output.close();
     socket.close();
     setVisible(false);
     dispose();
     break;
    }
   }
  } catch (Exception e) {
   System.out.println(e);
   JOptionPane.showMessageDialog(null,
     "Polaczenie sieciowe dla klienta zostalo przerwane");
   setVisible(false);
   dispose();
  }
 }

What I want to do is that my JComboBox klienciLista fill with new ArrayList of available clients, but that does not happen. After connecting, the server sends arrayList and client updates it but doesn't update ComboBox. Why is this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
TrN
  • 1,230
  • 2
  • 17
  • 32

5 Answers5

24

It's because you keep creating a new JComboBox in your loop, instead of updating the existing one.

Instead of

while(true){
...
klienciLista = new JComboBox(klienci.toArray());
...
}

do:

while(true){
    ...
    klienciLista.removeAllItems();
    for(String s:klienci){
        klienciLista.addItem(s);
    }
    ...
}

or, preferably, use a model:

    klienciLista.setModel(new DefaultComboBoxModel(klienci.toArray()));
dogbane
  • 266,786
  • 75
  • 396
  • 414
4

This is because you are creating a new JComboBox instead of updating the one on the GUI.

Look at the addItem() method on the JComboBox: http://download.oracle.com/javase/6/docs/api/javax/swing/JComboBox.html

jzd
  • 23,473
  • 9
  • 54
  • 76
3

First, you should create you JComboBox from a ComboBoxModel. Second, you shouldn't be calling new JComboBox inside the loop.

adrianboimvaser
  • 2,651
  • 1
  • 22
  • 30
1

Because when you do klienciLista = new JComboBox(klienci.toArray()); you are creating a new JComboBox and referencing it through that variable, but the original JComboBox still exists in the GUI. You have done nothing to change the JComboBox that is currently displayed.

masijade
  • 11
  • 1
  • 1
    As has already been suggested, use the addItem (if it is only adding items and not removing any) or better the setModel on the existing object already referenced by that variable rather than redefining that variable. – masijade Jan 20 '11 at 13:14
0

Clear and update your list not your comboBox.

noob
  • 1