我想在列表 cellrenderer 中精确地显示Label
s 和TextArea
a 。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 不增长呢?