动态编辑JCombobox中的项目 [英] Editing item in JCombobox dynamically
问题描述
我在这里有一个小问题. 我有一个带有许多不可编辑的JComboBox的Swing UI.每个JComboBox都是从数据库的特定表中填充的. 用户可以编辑每个表的内容,我想要做的就是根据用户在这些表中所做的操作(添加/删除/编辑)来更新主JFrame中的JComboBox.
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.
对于添加和删除项目,没问题,我从相关的JComboBoxes模型中添加或删除该项目,一切正常.
For adding and removing items, no problem, I add or remove that item from the related JComboBoxes model and everything goes fine.
但是对于编辑,它不起作用.我在JCombobox模型中添加了一个方法,可以用新项目替换以前的项目,但是无论我在该方法中执行什么操作(fireIntervalAdded(),fireIntervalRemoved()或两者以任意顺序),编辑后的项目都会以旧的形式出现状态.
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.
这是我的JComboBoxModels
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);
}
}
和
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). 有人可能会说我可以先做一个remove(oldString)然后再做add(newString),但后来我不能在列表中保持相同的位置. 有什么建议吗?
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??
推荐答案
代替...
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);
相反...
您当前的StringModel
似乎是一种浪费,因为DefaultComboBoxModel
已经拥有自己的支持模型.相反,您可以简单地从AbstractListModel
扩展并实现ComboBoxModel
,这将为您提供一个更干净的基类,例如...
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...
}
这篇关于动态编辑JCombobox中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!