Java Bean ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4218682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:22:03  来源:igfitidea点击:

Java Bean ArrayList

javaarraylistjavabeans

提问by Kamiikoneko

I have an ArrayList of Strings in my java bean. I want to be able to add/remove from this list at design time in the property editor. How do I implement this? I am coming back to Java after like 5 years so I'm a little stupid. I have managed to get all my simple type properties to be editable in the editor, but this property, when you click the "..." in the property editor, it says that custom editing is not supported for that type. So... Anyone able to point me in the right direction?

我的 java bean 中有一个字符串 ArrayList。我希望能够在设计时在属性编辑器中从该列表中添加/删除。我该如何实施?大约 5 年后我又回到 Java,所以我有点傻。我已经设法让我的所有简单类型属性都可以在编辑器中进行编辑,但是当您在属性编辑器中单击“...”时,该属性表示该类型不支持自定义编辑。所以......有人能指出我正确的方向吗?

Thanks a ton!!!

万分感谢!!!

回答by Denys P.

Mmm... Do you means this?

嗯……你是这个意思吗?

import java.util.List;

public MyBean {

private List<String> stringList;

public void add(String str) {
    stringList.add(str);
}

public String get(int i) {
    return stringList.get(i);
}

public String[] getElements() {
    String[] elements;
    stringList.toArray(elements);        
    return elements;
}

// ... Your bean struct here

}

or you need this (JList).

或者你需要这个 (JList)

回答by M.Stramm

Unfortunately the Java Beans Specdoes not mention Collections of any kind, however it supports Arrays as "Indexed Properties".

不幸的是,Java Beans 规范没有提到任何类型的集合,但是它支持将数组作为“索引属性”。

The pattern is:

图案是:

Type[] getFoo();
void setFoo(Type[] value);
Type getFoo(int index);
void setFoo(int index, Type[] value);

Popular components like JList and JTree get around this limitation by implementing a model (which is technically just a property) and a custom component editor to visually edit the model.

流行的组件(如 JList 和 JTree)通过实现模型(技术上只是一个属性)和自定义组件编辑器来可视化编辑模型来解决此限制。

There is a java trailfor custom editors which explains this in detail. I am unsure whether it is possible to create an editor for a get/set property of type ArrayList<T>, but probably that is also possible.

有一个用于自定义编辑器的java 路径,详细解释了这一点。我不确定是否可以为 type 的 get/set 属性创建编辑器ArrayList<T>,但这可能也是可能的。

回答by neerrrr

class TheBean {
    private List strings = ....

getStrings(){
    return Collections.unmodifiableList(this.strings) // so if you return the reference people cant muck with it.
}

addListElement(String toAdd) {
    // possibly validate toAdd
    this.strings.add(toAdd);
}
// you also need removeListelement
}

回答by hvgotcodes

Im not sure what you mean by property editor. Are you talking about an IDE?

我不确定您所说的属性编辑器是什么意思。你说的是IDE吗?

In any event, if your List is private, you have 2 options for allowing access to it.

无论如何,如果您的列表是私有的,您有 2 个选项允许访问它。

1) Return a reference to the list, which a caller can then operate on.
2) Create a method add/removeListElement, and pass in the Strings you want to add or remove.

1) 返回对列表的引用,然后调用者可以对其进行操作。
2)创建方法add/removeListElement,传入要添加或删除的Strings。

I like option 2 better, because it provides better encapsulation/data hiding of the List.

我更喜欢选项 2,因为它提供了更好的 List 封装/数据隐藏。

Something like the following, which is pseudo but you get the idea....

像下面这样的东西,这是伪的,但你明白了......

class TheBean {
    private List strings = ....

    getStrings(){
        return Collections.unmodifiableList(this.strings) // so if you return the reference people cant muck with it.
    }

    addListElement(String toAdd) {
        // possibly validate toAdd
        this.strings.add(toAdd);
    }
    // you also need removeListelement
}