Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在jlist中显示集合_Java_Arrays_Swing_Collections_Jlist - Fatal编程技术网

Java 在jlist中显示集合

Java 在jlist中显示集合,java,arrays,swing,collections,jlist,Java,Arrays,Swing,Collections,Jlist,我有一些学校,它们是其他班级的一系列学校对象 我无法在JList中添加和显示此阵列 public class SchoolChooser extends JPanel { private School[] schools; 有什么帮助吗? 谢谢。阅读更多关于JList in的信息 下面是一个简单的例子。我使用JList和DefaultListModel以及基于DefaultListCellRenderer的自定义渲染器。我写了我自己的班级,用你的班级代替 class Examp

我有一些学校,它们是其他班级的一系列学校对象

我无法在JList中添加和显示此阵列

 public class SchoolChooser extends JPanel {

      private School[] schools;
有什么帮助吗? 谢谢。

阅读更多关于JList in的信息

下面是一个简单的例子。我使用
JList
DefaultListModel
以及基于
DefaultListCellRenderer
的自定义渲染器。我写了我自己的班级,用你的班级代替

class Example extends JFrame {

    private DefaultListModel<School> model;
    private School[] schools;

    public Example() {
        schools = new School[]{
            new School("test1",1),  
            new School("test2",2),
            new School("test3",3),
        };
        JList<School> list = new JList<>(model = new DefaultListModel<>());
        for(School school : schools){
            model.addElement(school);
        }
        list.setCellRenderer(getCellRenderer());
        add(new JScrollPane(list));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

     private ListCellRenderer<? super School> getCellRenderer() {
        return new DefaultListCellRenderer(){
            @Override
            public Component getListCellRendererComponent(JList<?> list,
                    Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                School s = (School) value;
                Component listCellRendererComponent = super.getListCellRendererComponent(list, s.getNumber()+"/"+s.getName(), index, isSelected,cellHasFocus);
                return listCellRendererComponent;
            }
        };
    }

    public static void main(String...strings ){
            new Example();
     }

}
结果是:

public class School {

    private String name;
    private Integer number;

    public School(String name, Integer number){
        this.setName(name);
        this.setNumber(number);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

}