Editing item in JCombobox dynamically

二次信任 提交于 2019-12-24 12:33:41

问题


I have a small problem here. I have a Swing UI with many non editable JComboBoxes. Each of the JComboBoxes are populated from a specific table from a database. The user can edit the content of each table, and what I would like to do is to update the JComboBoxes in the main JFrame according to the action (add/remove/edit) the user just did in these tables.

For adding and removing items, no problem, I add or remove that item from the related JComboBoxes model and everything goes fine.

But for editing, it doesn't work. I have added a method in my JCombobox model to replace a previous item with a new item, but whatever I do in this method (fireIntervalAdded(), fireIntervalRemoved(), or both of them in any order) the edited item appears in its old state.

Here are my JComboBoxModels:

public class StringModel extends DefaultComboBoxModel
{
  private ArrayList<String> lstStrings;

  public StringModel()
  {
    super();
    lstStrings = new ArrayList<String>();
  }

  public StringModel(ArrayList<String> lstStrings)
  {
    super();
    lstStrings = new ArrayList<String>();

    for (String string : lstStrings)
    {
      lstStrings.add(string);
    }
  }

  protected ArrayList<String> getStrings()
  {
    return lstStrings;
  }

  public String getSelectedString()
  {
    return (String) getSelectedItem();
  }

  public void setSelectedString(String string)
  {
    setSelectedItem(string);
  }

  public Object getElementAt(int index)
  {
    return lstStrings.get(index);
  }

  public int getSize()
  {
    return lstStrings.size();
  }

  public int getIndexOf(Object element)
  {
    return lstStrings.indexOf(element);
  }
}

and

public class ModifiableStringModel extends StringModel
{
  public ModifiableStringModel()
  {
    super();
  }

  public ModifiableStringModel(ArrayList<String> lstStrings)
  {
    super(lstStrings);
  }

  public void clearStrings()
  {
    int oldSize = getStrings().size();
    getStrings().clear();
    fireIntervalRemoved(this, 0, oldSize);
  }

  public void addString(String string)
  {
    getStrings().add(string);
    int size = getStrings().size();
    fireIntervalAdded(this, size, size);
  }

  public void removeString(String string)
  {
    int position = getStrings().indexOf(string);
    getStrings().remove(position);
    fireIntervalRemoved(this, position, position);
  }

  public void replaceString(String oldString, String newString)
  {
    int position = getStrings().indexOf(oldString);
    getStrings().remove(position);
    fireIntervalRemoved(this, position, position);
    getStrings().add(position, newString);
    fireIntervalAdded(this, position, position);
  }

  public void removeAllStrings()
  {
    int positionStart=0;
    int positionEnd = getStrings().size();
    getStrings().clear();
    fireIntervalRemoved(this, positionStart, positionEnd);
  }

  public ModifiableStringModel getModl()
  {
    return this;
  }
}

The problematic method is replaceString(String oldString, String newString). One could argue that I could first make a remove(oldString) and then add(newString) but then I cannot keep the same position for the item in the list. Any advice??


回答1:


Instead of ...

getStrings().remove(position);
fireIntervalRemoved(this, position, position);
getStrings().add(position, newString);
fireIntervalAdded(this, position, position);

Which has a number of efficiency issues related to it, you could try...

getStrings().set(position, newString);
fireContentsChanged(this, position, position);

instead...

You current StringModel seems like a waste, as DefaultComboBoxModel already has a backing model of it's own. Instead, you could simple extend from AbstractListModel and implement ComboBoxModel which would give you a cleaner base class to start from, for example...

public class StringComboBoxModel extends AbstractListModel<String> implements ComboBoxModel<String> {

    private List<String> values;
    private String selectedItem;

    public StringComboBoxModel() {
        this(new ArrayList<String>(25));
    }

    public StringComboBoxModel(List<String> values) {
        this.values = values;
    }

    @Override
    public int getSize() {
        return values.size();
    }

    @Override
    public String getElementAt(int index) {
        return values.get(index);
    }

    @Override
    public void setSelectedItem(Object anItem) {
        if (anItem instanceof String) {
            selectedItem = (String) anItem;
        } else {
            selectedItem = null;
        }
    }

    @Override
    public Object getSelectedItem() {
        return selectedItem;
    }

    protected List<String> getValues() {
        return values;
    }

}

public class MutableStringComboBoxModel extends StringComboBoxModel {

    public MutableStringComboBoxModel() {
    }

    public MutableStringComboBoxModel(List<String> values) {
        super(values);
    }

    public boolean contains(String value) {
        return getValues().contains(value);
    }

    public void addValue(String value) {
        getValues().add(value);
        fireIntervalAdded(this, getSize() - 1, getSize() - 1);
    }

    public void replaceString(String oldString, String newString) {
        if (contains(oldString)) {
            int position = getValues().indexOf(oldString);
            getValues().set(position, newString);
            fireContentsChanged(this, position, position);
        } else {
            addValue(newString);
        }
    }

    // Other management methods...
}



回答2:


As pointed by MadProgrammer, using ArrayList.set() and then fireContentsChanged() perfectly solves the problem at hand. By the way, subclassing DefaultComboBoxModel 2 times was not a good idea, so I kind of made an hybrid of both which solves some problems related with that. So here is the new JComboBox model with the related replaceString() method :

public class HybridComboModel extends DefaultComboBoxModel
{
    private ArrayList<String>   lstStrings;

    public HybridComboModel()
    {
        super();
        lstStrings = new ArrayList<String>();
    }

    public HybridComboModel(ArrayList<String> theListofStrings)
    {
        super();
        lstStrings = new ArrayList<String>();

        for (String string : theListofStrings)
            {
                lstStrings.add(string);
            }
    }

    protected ArrayList<String> getStrings()
    {
        return lstStrings;
    }

    public String getSelectedString()
    {
        return (String) getSelectedItem();
    }

    public void setSelectedString(String string)
    {
        setSelectedItem(string);
    }

    public String getElementAt(int index)
    {
        return lstStrings.get(index);
    }

    public int getSize()
    {
        return lstStrings.size();
    }

    public int getIndexOf(String element)
    {
        return lstStrings.indexOf(element);
    }

    public void clearStrings()
    {
        int oldSize = getStrings().size();
        getStrings().clear();
        fireIntervalRemoved(this, 0, oldSize);
    }

    public void addString(String string)
    {
        getStrings().add(string);
        int size = getStrings().size();
        fireIntervalAdded(this, size, size);
    }

    public void removeString(String string)
    {
        int position = getStrings().indexOf(string);
        getStrings().remove(position);
        fireIntervalRemoved(this, position, position);
    }

    public void replaceString(String oldString, String newString)
    {
        int position = getStrings().indexOf(oldString);
        getStrings().set(position, newString);
        fireContentsChanged(this, position, position);
    }

    public void removeAllStrings()
    {
        int positionStart = 0;
        int positionEnd = getStrings().size();
        getStrings().clear();
        fireIntervalRemoved(this, positionStart, positionEnd);
    }
}


来源:https://stackoverflow.com/questions/24835552/editing-item-in-jcombobox-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!