简体   繁体   English

如何从ArrayList更新JComboBox内容?

[英]How to update JComboBox content from ArrayList?

I have JComboBox based on ArrayList: 我有基于ArrayList的JComboBox:

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: 客户端通过套接字在线程中获取新的ArrayList:

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. 我想做的是在我的JComboBox klienciLista中填充可用客户端的新ArrayList,但这不会发生。 After connecting, the server sends arrayList and client updates it but doesn't update ComboBox. 连接后,服务器将发送arrayList,客户端将对其进行更新,但不会更新ComboBox。 Why is this? 为什么是这样?

It's because you keep creating a new JComboBox in your loop, instead of updating the existing one. 这是因为您一直在循​​环中创建一个新的JComboBox,而不是更新现有的JComboBox。

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()));

This is because you are creating a new JComboBox instead of updating the one on the GUI. 这是因为您要创建一个新的JComboBox而不是在GUI上对其进行更新。

Look at the addItem() method on the JComboBox: http://download.oracle.com/javase/6/docs/api/javax/swing/JComboBox.html 查看JComboBox上的addItem()方法: http : //download.oracle.com/javase/6/docs/api/javax/swing/JComboBox.html

First, you should create you JComboBox from a ComboBoxModel. 首先,您应该从ComboBoxModel创建JComboBox。 Second, you shouldn't be calling new JComboBox inside the loop. 其次,您不应在循环内调用新的JComboBox。

Because when you do klienciLista = new JComboBox(klienci.toArray()); 因为当您执行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. 您正在创建一个新的JComboBox并通过该变量对其进行引用,但是原始 JComboBox 仍存在于GUI中。 You have done nothing to change the JComboBox that is currently displayed. 您没有做任何更改当前显示的JComboBox的操作。

清除并更新您的列表,而不是您的comboBox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM