Multiple lines in JList java

懵懂的女人 提交于 2019-12-08 14:24:36

问题


I'm currently working on a school project regarding school administration. In my project it is possible to create a school, education and students. I'm currently adding them all into the same JList(School), and it is giving me one problem. Whenever I'm adding for example a student it goes into the same line on the JList which makes it too long and I'm hoping for a way to do something like this on it:

School 1
 - Education 1
   - Student 1
   - Student 2
 - Education 2
   - Student 3
School 2
etc.
etc.

I've been reading about using cell renderer but I'm not sure that it works since I'm not creating a table. Please advice me about what to use.

Regards Jakob

Btw I don't want to use TextArea since I want to challenge myself.


回答1:


"I've been reading about using cell renderer but I'm not sure that it works since I'm not creating a table. "

There are different renderers for different components. JList has one and so does a JTabele You need to use a ListCellRender, which is a renderer for a JList cell. The renderer uses a Component to contain its content. With Component you can't use the next line carriage "\n", so you need to use HTML.

You can see more here ListCellRenderer

Here's an example, where I use a Student Object as the value for the list

class Student {
    String name;
    int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() { return name; }
    public int getAge() { return age; }
}

And here is the part of the render where I set the text

@Override
public Component getListCellRendererComponent(
        JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus) {
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    Student label = (Student) value;
    String name = label.getName();
    int age = label.getAge();
    String labelText = "<html>" + name + "<br/>" + age;
    setText(labelText);

    return this;
}

Here's the complete example

import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingUtilities;


public class Test3 {

    JList<Student> list;
    DefaultListModel model;

    public Test3() {
        list = new JList();
        model = new DefaultListModel();
        for (int i = 0; i < 10; i++) {
            model.addElement(new Student("Paul" + i, i));
        }
        list.setModel(model);
        list.setCellRenderer(new MyListCellRenderer());

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(list);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test3();
            }
        });
    }

    private class MyListCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(
                JList list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            Student label = (Student) value;
            String name = label.getName();
            int age = label.getAge();
            String labelText = "<html>Name: " + name + "<br/>Age: " + age;
            setText(labelText);

            return this;
        }

    }
}

class Student {
    String name;
    int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() { return name; }
    public int getAge() { return age; }
}



来源:https://stackoverflow.com/questions/21501770/multiple-lines-in-jlist-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!