Java Swing JList

发布于 2024-08-13 08:04:03 字数 128 浏览 4 评论 0原文

我正在尝试制作一个 JList 来显示数组的内容。然而,数组本身不是字符串数组,而是对象数组。

那么是否可以使用相同的对象数组作为参数来构造我的 JList(如果为对象提供了 toString 方法)?

谢谢。

I'm trying to make a JList to display the contents of an array. The array itself is not an array of strings, however, but an array of Objects.

Is it possible then, to use the same array of objects as the parameter to construct my JList (if the Objects are given a toString method)?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假情假意假温柔 2024-08-20 08:04:03

是的。

列表使用称为单元格渲染器的对象来显示其每个项目。默认单元格渲染器知道如何显示字符串和图标,并通过调用 toString 显示对象。

请参阅: http://java.sun.com/docs/ books/tutorial/uiswing/components/list.html

Yes it is.

A list uses an object called a cell renderer to display each of its items. The default cell renderer knows how to display strings and icons and it displays Objects by invoking toString.

See: http://java.sun.com/docs/books/tutorial/uiswing/components/list.html

¢蛋碎的人ぎ生 2024-08-20 08:04:03

JList 是一个基于 MVC 的控件,与 Swing 的其余部分一样。您可以向 JList 传递一个对象数组、字符串数组或向量,然后提供 ListCellRenderer 来呈现您传入的对象(默认情况下 DefaultListRenderer 使用(这只是一个 JLabel))。

如果不传入字符串,不设置 ListRender 来渲染对象,我不知道默认行为是什么。这是不好的做法。为了安全起见,很容易重写 DefaultListReender 来调用 Object.ToString() 。

在这里查看更多 JList 信息:
http://java.sun.com/products/jfc/ tsc/tech_topics/jlist_1/jlist.html

The JList is a MVC based control, like the rest of Swing. You can pass the JList an object array, a string array, or a vector and then supply a ListCellRenderer to render it the objects that you passed in (by default the DefaultListRenderer is used (which is just a JLabel)).

I don't know what the default behavior if you don't set a ListRender to render the object if you don't pass in strings. It's bad practice. Easy enough to override DefaultListRenender to call Object.ToString() to be safe.

See here more JList info:
http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文