简体   繁体   English

在 JList (Swing) 中显示 ArrayList

[英]Display a ArrayList with in a JList (Swing)

I'm having an issue when I try to display a JList with an ArrayList.当我尝试使用 ArrayList 显示 JList 时遇到问题。 I'm using Action Listeners to execute all this:我正在使用 Action Listeners 来执行所有这些:

ContactArray contactObject = new ContactArray();
addContactBtn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String firstName = firstNameField.getText();
        String lastName = lastNameField.getText();
        contactObject.addName(firstName + " " + lastName);
        // contactObject.getNames().forEach(System.out::println);
    }
});
viewContactButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String[] contacts = contactObject.getNames().toArray(new String[0]);
        contactList = new JList(contacts);
        contactList.setVisibleRowCount(5);
        contactList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        add(new JScrollPane(contactList));
    }
});

ContactArray class: ContactArray 类:

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ContactArray {

    private List<String> names;

    public ContactArray() {
        this.names = new ArrayList<>();
    }

    //add a name to list
    public void addName(String name) {
        if (!Objects.nonNull(names)) {
            this.names = new ArrayList<>();
        }
        this.names.add(name);
    }

    //get the name attribute
    public List<String> getNames() {
        if (!Objects.nonNull(names)) {
            this.names = new ArrayList<>();
        }
        return this.names;
    }

}

I have managed to print the full names to the console with contactObject.getNames().forEach(System.out::println);我已经设法使用contactObject.getNames().forEach(System.out::println);将全名打印到控制台contactObject.getNames().forEach(System.out::println); which I put in comments, but can't seem to add them to JList.我发表了评论,但似乎无法将它们添加到 JList。 Normally, when I press on viewContactButton , it should display it.通常,当我按下viewContactButton 时,它应该显示它。

Also, I'm using the Swing GUI form from IntelliJ IDEA.另外,我正在使用 IntelliJ IDEA 的 Swing GUI 表单。

Thanks for any help :)谢谢你的帮助 :)

I have managed to print the full names to the console我已经设法将全名打印到控制台

Well, you placed that code in the wrong place.好吧,您将该代码放在错误的位置。 The code should be placed when you actually use the List to create the JList.实际使用List 创建JList 时应放置代码。 (ie. Maybe you have code somewhere that accidentally deletes the List sometime after it is created and before it is used) (即。也许您在某处有代码在创建列表之后和使用之前的某个时间意外删除了列表)

I'm having an issue when I try to display a JList with an ArrayList当我尝试使用 ArrayList 显示 JList 时遇到问题

Well is the problem the ArrayList or did you also try to hardcode data in the JList?那么问题是 ArrayList 还是您也尝试在 JList 中对数据进行硬编码? In order to solve a problem you need to know what the real problem is.为了解决问题,您需要知道真正的问题是什么。 Always first try to display hard coded data instead of dynamic data.总是首先尝试显示硬编码数据而不是动态数据。

add(new JScrollPane(contactList));

I would guess the real problem is the above statement.我猜真正的问题是上面的语句。

Whenever you add components to a visible frame the basic code should be:每当您将组件添加到可见框架时,基本代码应该是:

add(...);
revalidate();
repaint();

You need to invoke the layout manager of the panel.您需要调用面板的布局管理器。 Otherwise the component has a size of 0, so there is nothing to paint.否则组件的大小为 0,所以没有什么可绘制的。

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

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