重新调整 JList 元素内的文本

Readjust text inside of JList element

我有一个JList,列表中充满了一个文件的元素,我希望将元素中的文本调整为列表的大小,增加单元格的高度并给出换行符。

我该怎么做?

I want the text within the elements to be adjusted to the size of the list

列表单元格渲染器(通常是 JLabel)支持 HTML 格式,因此我们可以使用样式来设置正文宽度。单元格的高度将相应调整。中间列表使用限制为 100 像素宽度的渲染器。

这是基于前 1000 个 Unicode 字符的属性的三个列表。每个列表的宽度都是为了显示列表模型中最宽的字符串(但是它的格式是为了呈现)。

这是 MCVE:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Vector;

public class UnicodeNameList {

    private JComponent ui = null;

    UnicodeNameList() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        Vector<String> unicodeNames = new Vector<>();
        Vector<String> unicodeDir = new Vector<>();
        Vector<String> unicodeChar = new Vector<>();
        for (int ii=0; ii<1000; ii++) {
            unicodeChar.add(new String(Character.toChars(ii)));
            unicodeNames.add(Character.getName(ii));
            unicodeDir.add("" + Character.getDirectionality(ii));
        }
        ui.add(new JScrollPane(new JList(unicodeChar)), BorderLayout.LINE_START);
        JList list = new JList(unicodeNames);
        LongListCellRenderer llcr = new LongListCellRenderer();
        list.setCellRenderer(llcr);
        ui.add(new JScrollPane(list));
        ui.add(new JScrollPane(new JList(unicodeDir)), BorderLayout.LINE_END);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            UnicodeNameList o = new UnicodeNameList();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

class LongListCellRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        String pre = "<html><body style='width: 100px;'>";
        JLabel l = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        String s = value==null ? "Null" : value.toString();
        l.setText(pre + s);
        return l;
    }
}