Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update JComboBox content from ArrayList?

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?

like image 925
TrN Avatar asked Jan 20 '11 12:01

TrN


People also ask

Which event gets generated when you select an item from a JComboBox?

Which event gets generated when you select an item from a JComboBox? Combo boxes also generate item events, which are fired when any of the items' selection state changes.

What is the difference between JComboBox and JList box give one method of each of them?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.


1 Answers

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()));
like image 63
dogbane Avatar answered Sep 20 '22 15:09

dogbane