我在 NetBeans 编辑器区域中有一个简单的 OutlineView,它显示了两列。第二列单元格的内容应可通过 PropertySupport 使用自定义属性编辑器进行设置。定制属性编辑器包含一个允许多项选择的 JList。
PropertySupport 类看起来像
public class CityProperty extends PropertySupport.ReadWrite<String> {
Customer c;
public CityProperty(Customer c, HashMap<String, Boolean> optionalCities) {
super("city", String.class, "City", "Name of City");
setValue("labelData", optionalCities);
this.c = c;
}
@Override
public String getValue() throws IllegalAccessException, InvocationTargetException {
return c.getCity();
}
@Override
public PropertyEditor getPropertyEditor() {
return new CityPropertyEditor(c);
}
@Override
public void setValue(String newValue) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
c.setCity(newValue);
}
}
PropertyEditor 看起来像
public class CityPropertyEditor extends PropertyEditorSupport implements ExPropertyEditor {
Customer c;
PropertyEnv env;
public CityPropertyEditorPanel editor = null;
public CityPropertyEditor(Customer c) {
this.editor = new CityPropertyEditorPanel();
this.c = c;
}
@Override
public String getAsText() {
String s = (String) getValue();
if (s == null) {
return "No City Set";
}
return s;
}
@Override
public void setAsText(String s) {
setValue(s);
}
@Override
public void attachEnv(PropertyEnv env) {
this.env = env;
}
@Override
public Component getCustomEditor() {
HashMap<String, Boolean> cities = (HashMap<String, Boolean>) env.getFeatureDescriptor().getValue("labelData");
DefaultListModel model = new DefaultListModel();
/* selection in the gui */
int[] selectedIdxs = new int[cities.size()];
int idx = 0;
for (String str : cities.keySet()) {
model.addElement(str);
if (cities.get(str) == Boolean.FALSE) {
selectedIdxs[idx] = model.indexOf(str);
idx++;
}
}
if (selectedIdxs.length > 0){
editor.jList.setSelectedIndices(selectedIdxs);
}
editor.jList.setModel(model);
return editor;
}
@Override
public boolean supportsCustomEditor() {
return true;
}
@Override
public Object getValue() {
System.out.println("getValue(): " + editor.jList.getSelectedValuesList());
System.out.println("getValue(): " + editor.jtf.getText());
return super.getValue();
}
}
并且编辑器 CityPropertyEditorPanel() 本身是一个简单的 JPanel,带有一个 JList 和一个 JTextField。
我的代码创建了一个很好的自定义编辑器,其中列出了所有项目,但它没有从列表中返回新选择的项目。我现在的问题是,如何将 JList 中的选定项目返回 CityProperty 类?我的尝试是使用
editor.jList.getSelectedValuesList());
在 getValue() 方法中,但结果始终为空。JTextField 也是如此,新的写入值也不会传回。
我在这里做错了什么?