java JList 列出对象的 ArrayList 的 toString()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10320325/
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-31 00:33:12  来源:igfitidea点击:

JList to list toString() of an ArrayList of Objects

javaswinguser-interfacearraylistjlist

提问by DanMc

I have an ArrayList of objects (A class called OrderItem). OrderItem has a toString() method in it.

我有一个对象的 ArrayList(一个名为 OrderItem 的类)。OrderItem 中有一个 toString() 方法。

I also have a GUI class in which I have a JList. I want to list all the toString()'s for the elements of the arrayLists.

我还有一个 GUI 类,其中有一个 JList。我想列出 arrayLists 元素的所有 toString()。

I know that for an arrayList of strings you can make them show in a JList by using:

我知道,对于字符串数组列表,您可以使用以下命令将它们显示在 JList 中:

ArrayList<String> myArrayList  = new ArrayList<String>();
myArrayList.add("Value1");
myArrayList.add("Value2");
myJList = new JList(myArrayList.toArray());

But I want to list the toString() methods of an object arrayList, i.e. have something like:

但我想列出对象arrayList的toString()方法,即有类似的东西:

ArrayList<OrderItem> myArrayList  = new ArrayList<OrderItem>();
myJList = new JList(myArrayList.toString());

I realise that there is a possibility that JList doesn't support such a feature or that there is some sort of logic problem with this. If that is so could you inform me as to why? Because surely an arrayList of strings should work in a similar way to an object arrayList's toString(). I merely want to be pulling out a String value for the elements and using those values for my list.

我意识到 JList 可能不支持这样的功能,或者这存在某种逻辑问题。如果是这样,你能告诉我为什么吗?因为字符串的arrayList 肯定应该以类似于对象arrayList 的toString() 的方式工作。我只想为元素提取一个字符串值并将这些值用于我的列表。

I've searched the web for an answer and have not been able to find one that helps me, so I've come here to try to get this resolved.

我在网上搜索了一个答案,但没有找到对我有帮助的答案,所以我来这里是为了解决这个问题。

Thanks a lot!

非常感谢!

回答by tenorsax

By default, JListshows the toStringvalue of the object. So there is no need to convert your objects to strings.

默认情况下,JList显示toString对象的值。因此无需将对象转换为字符串。

You can override that behavior of JListif needed by creating custom cell renderers. See How to Use Listsfor more details.

JList如果需要,您可以通过创建自定义单元格渲染器来覆盖该行为。有关更多详细信息,请参阅如何使用列表

回答by evanwong

You can convert the list to an array and then put it in the list.

您可以将列表转换为数组,然后将其放入列表中。

ArrayList<OrderItem> myArrayList  = new ArrayList<OrderItem>();
OrderItem[] items = new OrderItem[myArrayList.size()];
myArrayList.toArray(items);
myJList = new JList(items);

回答by user3872361

Actually, the toArray() method displays each item in the arrayList. JList doesn't show anything by default. You have to set the visibility according to your needs.

实际上,toArray() 方法显示了arrayList 中的每一项。JList 默认不显示任何内容。您必须根据需要设置可见性。

It sounds like you are trying to simply display the object's toString method in the list instead of the full object; in other words, you simply want to display the string representation. instead of the array representation.

听起来您试图简单地在列表中显示对象的 toString 方法而不是完整的对象;换句话说,您只想显示字符串表示。而不是数组表示。

You can rewrite the array to represent the data you want to show (provide the "toString construct as the array is built):

您可以重写数组以表示要显示的数据(在构建数组时提供“toString 构造”):

ArrayList<> myNewArrayList =  new ArrayList<OrderItem>();
for (int i=0; i< oldArray.size(); i++)
{    
    newArray.add(oldArray.get)i).toString(); 
}
    ...  rest of code  ...

Then use the new array in the panel and use the index as reference to the object array for object processing.

然后在面板中使用新的数组,并使用索引作为对象数组的引用进行对象处理。

HTH LLB

法学学士