我这里有一个小问题。 我有一个 Swing UI,其中有许多不可编辑的 JComboBox。每个 JComboBox 都是从数据库中的特定表填充的。 用户可以编辑每个表的内容,我想做的是根据用户刚刚在这些表中执行的操作(添加/删除/编辑)来更新主 JFrame 中的 JComboBoxes。
对于添加和删除项目,没问题,我从相关的 JComboBoxes 模型中添加或删除该项目,一切都很顺利。
但是对于编辑来说,它不起作用。我在 JCombobox 模型中添加了一个方法,用新项目替换以前的项目,但无论我在此方法中执行什么操作(fireIntervalAdded()、fireIntervalRemoved() 或按任意顺序两者),编辑的项目都会显示为旧状态。
这是我的 JComboBoxModel:
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);
}
}
和
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;
}
}
有问题的方法是replaceString(String oldString, String newString)。 有人可能会说我可以先删除(oldString),然后添加(newString),但随后我无法为列表中的项目保留相同的位置。 有什么建议吗??
最佳答案
而不是...
getStrings().remove(position);
fireIntervalRemoved(this, position, position);
getStrings().add(position, newString);
fireIntervalAdded(this, position, position);
其中存在许多与之相关的效率问题,您可以尝试...
getStrings().set(position, newString);
fireContentsChanged(this, position, position);
相反...
您当前的StringModel
似乎是一种浪费,因为DefaultComboBoxModel
已经有它自己的支持模型。相反,您可以简单地从 AbstractListModel
扩展并实现 ComboBoxModel
,这将为您提供一个更清晰的基类,例如......
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...
}
关于java - 动态编辑 JCombobox 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24835552/