简体   繁体   English

Java Swing DefaultListModel包含存储更多信息

[英]Java Swing DefaultListModel containing storing more information

I created a class to store two properties 我创建了一个用于存储两个属性的类

public class MailEntry {
    private String mail;
    private MailFormat format; // this is an enum

    public MailEntry(String mail, MailFormat format) {
        this.mail = mail;
        this.format = format;
    }

    public String getMail() {
        return mail;
    }

    public MailFormat getFormat() {
        return format;
    }
}

the JList created for me by the Netbeans GUI is declared by Netbeans GUI为我创建的JList由以下方法声明:

    private javax.swing.JList<String> jList1;

and initialized a DefaultListModel 并初始化了DefaultListModel

private DefaultListModel<MailEntry> listModel = new DefaultListModel<>();

and set this as a model 并设置为模型

jList1.setModel(listModel);

But what I get is 但是我得到的是

 error: incompatible types: DefaultListModel<MailEntry> cannot be converted to ListModel<String>
    jList1.setModel(listModel);

It seems that jList expects a model of Strings. 似乎jList期望使用Strings模型。 But I'd like to store more item-specific information, which will be accessible through the GUI. 但是我想存储更多特定于项目的信息,这些信息可以通过GUI访问。

How can I work it around? 我该如何解决?

The problem is you've decleared jList1 as... 问题是您将jList1 jList1为...

private javax.swing.JList<String> jList1;

but you're declaring the model as... 但您将模型声明为...

DefaultListModel<MailEntry> listModel = new DefaultListModel<>();

MailEntry and String are not compatible classes and the JList is expecting a ListModel<String> based model. MailEntryString是不兼容的类,并且JList需要基于ListModel<String>的模型。

You need to change the JList declaration to support your model, something like 您需要更改JList声明以支持您的模型,例如

private javax.swing.JList<MailEntry> jList1;

Since you're using Netbean's form editor (don't get me started), you will need to select the JList from the "Navigator" 由于您使用的是Netbean的表单编辑器(不帮助我入门),因此需要从“导航器”中选择JList

航海家

Select the "Code" tab from the "Properties" tab... 从“属性”标签中选择“代码”标签。

性质

and change the Type Parameters to meet your requirements 并更改Type Parameters以满足您的要求

private DefaultListModel<MailEntry> listModel = new DefaultListModel<>();

Should be: 应该:

private DefaultListModel<MailEntry> listModel = new DefaultListModel<MailEntry>();

And then when you create the JList you should be using: 然后在创建JList时应使用:

JList<MailEntry> list = new Jlist<MailEntry>();

So everything should be consistent. 所以一切都应该是一致的。

Note you should also need to create a custom renderer to display the data. 请注意,您还需要创建一个自定义渲染器以显示数据。 The default renderer only uses the toString() value of the class. 默认渲染器仅使用类的toString()值。 You can read the section from the Swing tutorial on How to Use Lists for more information and examples. 您可以阅读Swing教程中有关如何使用列表的部分, 获取更多信息和示例。

The other option is to just implement a toString() method in your class. 另一个选择是仅在您的类中实现toString()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM