문제

나는 수업을 기반으로합니다 Composite 그것은 SWT를 포함합니다 List 사례. 기본 설정을 사용하여 목록은 WINXP 시스템에서 5 행 높이입니다. 하드 코딩 된 픽셀 값 또는 DPI 설정 등에 의존하지 않고, 내부 마진이 추가되지 않고 목록 (및 주변 복합재)의 높이를 고정 된 수의 행으로 설정할 수 있습니까?

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);
}
도움이 되었습니까?

해결책

높이를 얻으려면 list.getBorderWidth () 및 list.getItemHeight ()를 사용할 수 없습니까?

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top