超级简单的 java swing jlist 问题,我就是想不出来!

发布于 2024-09-26 07:24:46 字数 431 浏览 5 评论 0原文

好的,我正在做作业,我正在使用 SWING 为 Java 项目制作 GUI,但我在使用 JList 时遇到了麻烦。

我已经创建了一个客户对象并为其设置了属性,我想将该对象添加到 TreeMap 中。我想连接树形图,以便地图中的任何和所有对象都将填充(无论如何是名称属性)JList 内部。

我做了很多调查,发现很多关于从头开始编写这些东西的内容,但很少涉及 Swing 实现。我将客户对象放入我的地图中,然后我希望我的 JList 能够反映地图的内容,但我不知道如何连接它。

    customers.put(c.getName(), c);
    this.customerList.(What can I do here? add Customer object?? I can't find what I need);

感谢您的帮助!!!

Ok, so I am working on a homework assignment, and I am using SWING to make a GUI for a Java project, and I am running into troubles with JList.

I have a customer object that I have made and set attributes to, and I want to add the object into a TreeMap. I want to hook up the Treemap so that any and all objects that are in the map will populate (the name attribute anyway) inside of the JList.

I have done a lot of looking around, and am seeing a lot about coding these things from scratch but little dealing with Swing implementation. I put my customer object into my map and then I would like for my JList to reflect the map's contents, but I don't know how to hook it up.

    customers.put(c.getName(), c);
    this.customerList.(What can I do here? add Customer object?? I can't find what I need);

Thanks for your help!!!

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

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

发布评论

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

评论(2

吻风 2024-10-03 07:24:47

您需要创建一个自定义列表模型,该模型返回要放入 JList 每一行中的对象。 TreeMap 无法通过索引访问,因此您需要其他东西。所以总体思路是这样的:(来自 JList javadoc ):

ListModel bigData = new AbstractListModel() {
    ArrayList customers;
    public int getSize() { return customers.size() }
    public Object getElementAt(int index) { return customers.get(index); }
};

JList bigDataList = new JList(bigData);

这样,当您更新集合时,只需在 JList 上调用 revalidate()repaint() ,它也会更新其内容。

You need to create a custom list model that returns objects to put in each row of a JList. TreeMap can't be accessed with an index, so you'll need something else. So the general idea is this: (from JList javadoc):

ListModel bigData = new AbstractListModel() {
    ArrayList customers;
    public int getSize() { return customers.size() }
    public Object getElementAt(int index) { return customers.get(index); }
};

JList bigDataList = new JList(bigData);

this way when you update your collection, just call revalidate() or repaint() on the JList and it will update its contents too.

同展鸳鸯锦 2024-10-03 07:24:47

所以我正在做作业
作业

到底是关于什么的呢?您已经向我们提供了您尝试的解决方案,但由于我们不知道实际要求,因此我们无法判断您是否走在正确的轨道上。

您是否被迫使用 TreeMap 来存储对象?因为这不是一个很好的用于 ListModel 的集合,因为您无法直接访问对象。

或者该赋值只是为了显示 JList 中对象的数据?如果是这样,那么您可以使用 DefaultListModel。您需要做的就是重写自定义对象的 toString() 方法,以使“名称属性”显示在列表中。

so I am working on a homework
assignment

So what exactly is the assignment about? You've given us your attempted solution, but since we don't know the actual requirement we can't tell if you are on the right track or not.

Are you forced to use a TreeMap to store the objects? Because this is not a good collection to use for your ListModel since you can't access the objects directly.

Or is the assignment simply about displaying data from an object in a JList? If so then you can use the DefaultListModel. All you need to do is override the toString() method of you custom object to have the "name attribute" show in the list.

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