为什么TextArea setGrowByContent(true)在包含在List单元格渲染器中时效率低下?

时间:2012-01-05 12:15:15

标签: java-me lwuit lwuit-textarea lwuit-label lwuit-list

我想在Label内显示TextAreaList,正好在列表cellrenderer中。问题是,即使我设置((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不会增长?

2 个答案:

答案 0 :(得分:2)

您需要了解列表渲染器。渲染具有统一的大小,由列表初始化或模型更改时相当复杂的逻辑确定。

查看ContainerList或只使用包含其中组件的Container。

答案 1 :(得分:1)

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