gpt4 book ai didi

java - 无法访问自定义属性编辑器中的 JList 项

转载 作者:行者123 更新时间:2023-11-29 09:16:02 24 4
gpt4 key购买 nike

我在 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 也是如此,新写入的值也不会传回。

我在这里做错了什么?

最佳答案

我想我找到了解决方案/解决方法。

当我激活 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本身有一个更新Map变量isoValNew的ListSelectionListener

    isoValueList.addListSelectionListener(new ListSelectionListener() {

@Override
public void valueChanged(ListSelectionEvent e) {
isoValNew.clear();

for (Object obj : isoValueList.getSelectedValues()) {
isoValNew.put((Double) obj, Boolean.TRUE);
}
}
});

我敢肯定这不是一个完美的解决方案,但对我来说效果很好。

希望这对某人有帮助。

关于java - 无法访问自定义属性编辑器中的 JList 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9393058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com