为什么 TextArea setGrowByContent(true) 当包含在列表单元格渲染器内时效率低下?

发布于 2024-12-25 01:38:21 字数 4815 浏览 1 评论 0原文

我想在 List 中显示 LabelTextArea ,恰好在列表 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 不增长?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海夕 2025-01-01 01:38:21

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

查看 ContainerList 或仅使用其中包含组件的容器。

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.

一花一树开 2025-01-01 01:38:21

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

@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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文