2009-06-17 59 views
4

我有一個基於Composite的課程,它嵌入了一個SWT List實例。使用默認設置,我的WinXP系統上的列表爲五行。不依賴硬編碼的像素值或DPI設置等,我怎樣才能將列表(和周圍組合)的高度設置爲固定行數(例如3),而不增加內部邊距?如何在行中設置SWT列表的高度?

public FileSetBox(Composite parent, int style) 
{ 
    super(parent, style); 

    setLayout(new FillLayout()); 

    this.list = new List(this, SWT.V_SCROLL); 

    ... 
} 

更新:

以下的作品,但它並沒有考慮到邊境,導致最後一行的部分被覆蓋增加的高度。任何想法如何計算呢?

public FileSetBox(Composite parent, int style) 
{ 
    ... 
    GC gc = new GC(this); 
    gc.setFont(this.list.getFont()); 
    this.preferredHeight = gc.getFontMetrics().getHeight() * 3; 
    gc.dispose(); 
    ... 
} 

@Override 
public Point computeSize(int arg0, int arg1) 
{ 
    Point size = super.computeSize(arg0, arg1); 
    return new Point(size.x, this.preferredHeight); 
} 

回答

4

你不能使用list.getBorderWidth()和list.getItemHeight()來獲取高度嗎?

+0

愚蠢的我......這很明顯,我忽略了它。非常感謝 :-) – 2009-06-18 06:28:16

1
 
public FileSetBox(Composite parent, int style) 
{ 
    super(parent, style); 

    setLayout(new GridLayout(1, false)); 

    this.list = new List(this, SWT.V_SCROLL); 

    GridData data = new GridData(GridData.FILL_BOTH); 
    data.heightHint = 10 * ((List)control).getItemHeight(); // height for 10 rows 
    data.widthHint = getStringWidth(25, list); // width enough to display 25 chars 
    list.setLayoutData(data); 

    ... 
} 

    public static int getStringWidth(int nChars, Control control){ 
     GC gc = new GC(control); 
     gc.setFont(control.getFont()); 
     FontMetrics fontMetrics = gc.getFontMetrics(); 
     gc.dispose(); 
     return nChars * fontMetrics.getAverageCharWidth(); 
    }