简体   繁体   English

在JList中添加并显示对象

[英]Add and show object in JList


I am working on a project where I am supposed to create an application that takes information and stores it in a JList, and when I select the name of a person in the JList it should then show the contact information of that person. 我正在一个项目上,我应该创建一个将信息存储在JList中的应用程序,当我在JList中选择一个人的名字时,它应该显示该人的联系信息。
I have made 2 classes - one of them in the contact class and the other is the one with the JFrame. 我做了2个类-其中一个在contact类中,另一个是使用JFrame的类。

This is the Contact class 这是联系人类

private String firstName;
private String lastName;
private String phNumber;
private String address;


public Contact(String firstName, String lastName, String phNumber, String address) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.phNumber = phNumber;
    this.address = address;
}


public String getFirstName() {
    return firstName;
}


public void setFirstName(String firstName) {
    this.firstName = firstName;
}


public String getLastName() {
    return lastName;
}


public void setLastName(String lastName) {
    this.lastName = lastName;
}


public String getPhNumber() {
    return phNumber;
}


public void setPhNumber(String phNumber) {
    this.phNumber = phNumber;
}


public String getAddress() {
    return address;
}


public void setAddress(String address) {
    this.address = address;
}


@Override
public String toString() {
    return firstName + " " + lastName;
}


@Override
public int compareTo(Object o) {
    if(((Contact) o).getFirstName().compareTo(this.firstName)>=1){
        return 1;
    }
    if(((Contact) o).getFirstName().compareTo(this.firstName)<=-1){
        return -1;
    }
    return 0;
}


}

Then this is the code for the add button I am working on. 这是我正在使用的添加按钮的代码。

JButton btnNewButton = new JButton("Save");
    btnNewButton.addActionListener(new ActionListener() {
        @SuppressWarnings({ "unchecked", "serial" })
        public void actionPerformed(ActionEvent e) {
            String f = txtFirst.getText();
            String l = txtLast.getText();
            String p = txtPhone.getText();
            String a = txtAddress.getText();

            list.add(new Contact(f, l, p, a));

            Object[] array = list.toArray();
            //listView is the name of the JList
            listView.setListData((Contact[]) array);

        }
    });
    btnNewButton.setBounds(244, 153, 89, 23);
    frame.getContentPane().add(btnNewButton);

I just need help adding the Contact class into the JList and that be able to display the the first and last name on the JList Thank you for helping me in advance 我只需要帮助将Contact类添加到JList中,并且能够在JList上显示名字和姓氏,谢谢您的帮助。

You can just add your Contact object to the JList and then create a custom renderer. 您可以只将Contact对象添加到JList中,然后创建一个自定义渲染器。

Something like: 就像是:

class ContactRenderer extends DefaultListCellRenderer
{
    public Component getListCellRendererComponent(
        JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus)
    {
        super.getListCellRendererComponent(list, value,
            index, isSelected, cellHasFocus);

        Contact contact = (Contact)value;
        setText(contact.getFirstName() + " " + contact.getLastName()


        return this;
    }
}

You assign the renderer to the Jlist using: 使用以下命令将渲染器分配给Jlist:

list.setCellRenderer( new ContactRenderer() );

Read the section from the Swing tutorial on Creating a Custom Renderer for more information and examples. 阅读Swing教程中有关创建自定义渲染器的部分,以获取更多信息和示例。 The example renderer is for a combo box, but the concept is the same. 示例渲染器用于组合框,但是概念相同。

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

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