The bottom line:

I can’t assign a list of my items to a button-list

there is

ArrayList<String> users = new ArrayList(); 

there is also a line in the class with a GUI

 jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"})); 

I work in NetBeans, I can’t assign my list to the list button. On foreign internet I found this:

 jComboBox1.removeAllItems(); for (int i = 0; i < users.size(); i++) { jComboBox1.addItem(users.get(i)); } 

PROBLEMS OF ATTACHED BELOW ANSWERS:

  jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"})); jComboBox1.removeAllItems(); String [] items = new String [10]; for (int i = 0; i < new ListFriend().nameSurname.size(); i++) { items[i] = new ListFriend().nameSurname.get(i); } jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(items)); 

As a result, I received the elements, but they are not displayed. What is the reason?

enter image description here

  jComboBox1.setModel(myModel); myModel.add("Hello"); myModel.add("Hel23lo"); myModel.add("Hell3wso"); for (int i = 0; i < new ListFriend().nameSurname.size(); i++) { String r = new ListFriend().nameSurname.get(i); myModel.add(r); } 

If you try this, it also does not work, it does not throw the elements of the array into the list, except for those that were added through just myModel.add, it does not allow you to add through a cycle.

If you even write this:

  for (int i = 0; i < 5; i++) { String r = "hello world"; new MyModel().add(r); } 

he will not add them anyway.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

2 answers 2

There are several ways to dynamically add elements to JComboBox . The most correct way is to create an object that implements the ComboBoxModel interface and pass it to JComboBox .

After this, adding and deleting elements should be done in this object, and JComboBox will display the changes automatically.

Here is a simple implementation for storing strings.

 public class MyModel extends AbstractListModel<String> implements ComboBoxModel<String> { private List<String> list; private String selected; public MyModel() { list = new ArrayList<>(); } public void remove(String item) { list.remove(item); fireContentsChanged(this, 0, list.size()); } public void add(String item) { list.add(item); fireContentsChanged(this, 0, list.size()); } @Override public int getSize() { return list.size(); } @Override public String getElementAt(int index) { return list.get(index); } @Override public void setSelectedItem(Object item) { selected = (String) item; } @Override public Object getSelectedItem() { return selected; } } 

Use it like this. Creature:

 MyModel myModel = new MyModel(); JComboBox<String> = new JComboBox<>(myModel); 

Add / remove items:

 myModel.add("hello"); myModel.add("world"); myModel.remove("hello"); // добавить элементы в цикле // поскольку мы храним строки, то при добавлении элемента его необходимо привести типу String for(int i = 0 ; i < 10 ; i++) { myModel.add("" + i); // сложение с пустой строкой один из способов приведения к String } 

If you have a ready list of strings, you can add it here.

 List<String> r = new ArrayList<>(); // предположим, что тут r заполняется значениями // добавляем значения в ComboBox for(String i: r) { myModel.add(i); } 

Here you can find a working sample application - SimpleMVC , although there I use JList , but the principle is the same.

  • I will attach the answer now, look at how to fix it below) - Marat Zimnurov
  • has attached the answer, look - Marat Zimnurov
  • I tried it with your code and with my own, the program seems to be working, but he doesn’t see them in the field list - Marat Zimnurov
  • The problem is that at your command, if you enter myModel.add ("Hello); it adds the values ​​to the list, but if you pull them out of myModel.add (ListArray (). get (i)); then it doesn't work - Marat Zimnurov
  • jComboBox1.setModel (myModel); myModel.add ("Hello"); myModel.add ("Hel23lo"); myModel.add ("Hell3wso"); for (int i = 0; i <new ListFriend (). nameSurname.size (); i ++) {String r = new ListFriend (). nameSurname.get (i); myModel.add (r); } if you try this, it also does not work, it does not throw the elements of the array into the list. - Marat Zimnurov

If javax.swing.JComboBox<E> about speech, then it takes JComboBox(E[] items) in the array constructor to JComboBox(E[] items) array of JComboBox(E[] items) .

 String[] items = { "item1", "item2", "item3" }; JComboBox comboBox = new JComboBox(items); 
  • the bottom line is that how can I push a dynamic array into an array of strings ... - Marat Zimnurov