我在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()本身是一个带有JList和JTextField的简单JPanel。
我的代码创建了一个很好的自定义编辑器,其中列出了所有项目,但它没有从列表中返回新选择的项目。我现在的问题是,如何将选定的项目从JList返回到CityProperty类?我的尝试是使用
editor.jList.getSelectedValuesList());
在getValue()方法中但结果始终为空。对于JTextField也是如此,其中新的写入值也不会被传回。
我在这里做错了什么?
答案 0 :(得分:0)
我想我找到了解决方案/解决方法。
当我激活PropertyEnv.STATE_NEEDS_VALIDATION功能时,CityPropertyEditor识别出“编辑器”对象的内容。然后CityPropertyEditor中的代码必须覆盖attacheEnv方法并包含VetoableChangeListener
@Override
public void attachEnv(PropertyEnv env) {
this.env = env;
env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
env.addVetoableChangeListener(new VetoableChangeListener() {
@Override
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
/* User has pushed OK */
for (Entry entry : editor.isoValNew.entrySet()){
isoVal.put((Double) entry.getKey(), (Boolean) entry.getValue());
}
}
});
}
而CityPropertyEditorPanel()中的Jlist本身有一个ListSelectionListener,它更新Map变量isoValNew
isoValueList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
isoValNew.clear();
for (Object obj : isoValueList.getSelectedValues()) {
isoValNew.put((Double) obj, Boolean.TRUE);
}
}
});
我确信这不是一个完美的解决方案,但在我的情况下它可以正常工作。
希望这有助于某人。