Why is TextArea setGrowByContent(true) inefficient when contained inside a List cell renderer?

229 views Asked by At

I want to display Labels and TextArea inside a List , precisely in the list cellrenderer. The problem is that even if I set ((TextArea)lData[i]).setGrowByContent(true); then the TextArea doesn't grow !

Here are codes :

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;
    }
}

The superclass's code :

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;
    }
}

So why doesn't the TextArea grow when the data is a long String data ?

2

There are 2 answers

0
Shai Almog On BEST ANSWER

You need to read about list renderers. Renders have a uniform size that is determined by rather complex logic at list initialization or model change.

Take a look at ContainerList or just use a Container with components within it.

0
Olofu Mark On

@Shai, I had the same problem too. there was no workaround and my custom list renderer had a table layout containing a text area and three labels. I think it is better(most consistent) to use a container with components in it (which scrolls quite slowly and tranfers focus lazily for large models).