1

我想在列表 cellrenderer 中精确地显示Labels 和TextAreaa 。List问题是即使我设置((TextArea)lData[i]).setGrowByContent(true);了 TextArea 也不会增长!

以下是代码:

public class CListCellEtapesProspection extends CListCell
{
    private Container cRowYAll = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    private Container row1 = new Container(new BoxLayout(BoxLayout.X_AXIS));
    private Container row2 = new Container(new BoxLayout(BoxLayout.X_AXIS));
    private Container row3 = new Container(new BoxLayout(BoxLayout.X_AXIS));
    private Label[] labels;
    private final int marginR = 5;
    private Font appliFont = MenuPrincipalForm.r.getFont("FontLibelle");
    private int largeurRestant;
    private Component[] lData;

    public CListCellEtapesProspection(String[] libelles, boolean displayHorizontalLine, int formW)
    {
        super(displayHorizontalLine);
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        labels = new Label[libelles.length];
        lData = new Component[libelles.length];
        for (int i=0;i<libelles.length;i++)
        {
            labels[i] = new Label(libelles[i]);
            labels[i].setUIID("ListLibelle");

            if (i != 2)
            {
                lData[i] = new Label("");
                lData[i].getStyle().setMargin(Component.TOP, 1);
                lData[i].getStyle().setMargin(Component.BOTTOM, 1);
            }
            else
            {
                lData[i] = new TextArea();
                ((TextArea)lData[i]).getUnselectedStyle().setFont(appliFont, false);
                ((TextArea)lData[i]).getSelectedStyle().setFont(appliFont, false);
                ((TextArea)lData[i]).setPreferredH(appliFont.getHeight()*3);
                ((TextArea)lData[i]).setGrowByContent(true);
            }
        }
        labels[0].setPreferredW(Comparator.max(new int[]{labels[0].getPreferredW(),labels[1].getPreferredW(),labels[2].getPreferredW()}));
        labels[1].setPreferredW(Comparator.max(new int[]{labels[0].getPreferredW(),labels[1].getPreferredW(),labels[2].getPreferredW()}));
        labels[2].setPreferredW(Comparator.max(new int[]{labels[0].getPreferredW(),labels[1].getPreferredW(),labels[2].getPreferredW()}));
        labels[0].getStyle().setMargin(Component.RIGHT, marginR, false);
        labels[1].getStyle().setMargin(Component.RIGHT, marginR, false);
        labels[2].getStyle().setMargin(Component.RIGHT, marginR, false);
        largeurRestant = formW - ( labels[0].getPreferredW() + marginR );
        ((TextArea)lData[2]).setPreferredW(largeurRestant);
        row1.addComponent(labels[0]);
        row1.addComponent(lData[0]);
        row2.addComponent(labels[1]);
        row2.addComponent(lData[1]);
        row3.addComponent(labels[2]);
        row3.addComponent(lData[2]);
        cRowYAll.addComponent(row1);
        cRowYAll.addComponent(row2);
        cRowYAll.addComponent(row3);
        addComponent(cRowYAll);
    }
    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
    {
        String datetime = null; // 13/05/2003 16h 30mn
        String datetimeFormated = null; // 13/05/2003 - 16h 30mn
        Content entry = null;
        if (value instanceof Content)
            entry = (Content)value;
        if (!"".equals(entry.getColumn(0)))
        {
            ((Label)lData[0]).setText(Formatage.nvl(entry.getColumn(1),"-"));
            datetime = entry.getColumn(2);
            if (!"".equals(datetime))
                datetimeFormated = datetime.substring(0, datetime.indexOf(" ")).concat(" - ").concat(datetime.substring(datetime.indexOf(" ")+1));
            if (!"".equals(datetime))
                ((Label)lData[1]).setText(datetimeFormated);
            else
                ((Label)lData[1]).setText("-");
            ((TextArea)lData[2]).setText(entry.getColumn(3));
        }
        else
        {
            ((Label)lData[0]).setText("-");
            ((Label)lData[1]).setText("-");
            ((TextArea)lData[2]).setText("");
        }
        list.repaint();
        return this;
    }
}

超类的代码:

public class CListCell extends Container implements ListCellRenderer {

    private Label focus = new Label("");

    public CListCell(boolean paintHorizontalLine)
    {
        focus.setUIID("bandeau_selection_list");
        if (paintHorizontalLine)
            getStyle().setBgPainter(new LigneHorizontalPainter(this, 13553358));
    }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
    {
        return this;
    }

    public Component getListFocusComponent(List arg0)
    {
        return focus;
    }
}

那么为什么当数据是长字符串数据时 TextArea 不增长呢?

4

2 回答 2

2

您需要阅读有关列表渲染器的信息。渲染具有统一的大小,由列表初始化或模型更改时相当复杂的逻辑决定。

看看 ContainerList 或者只是使用一个 Container 里面的组件。

于 2012-01-05T19:12:28.763 回答
1

@Shai,我也有同样的问题。没有解决方法,我的自定义列表渲染器有一个包含文本区域和三个标签的表格布局。我认为使用包含组件的容器会更好(最一致)(对于大型模型,它滚动非常缓慢并且延迟转移焦点)。

于 2012-05-29T20:22:36.117 回答