[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: limiting tree control height with formlayout

Hi Matt,

Putting your code into a snippet (with minor changes that should not make a
difference) seems to work for me, the snippet is pasted below.  Maybe it's
not a problem with your Tree's FormData, but with its parent Composite?

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());

    Tree dbTree = new Tree(shell, SWT.SINGLE |  SWT.V_SCROLL);
    for (int i = 0; i < 99; i++) {
        TreeItem root = new TreeItem(dbTree, SWT.NONE);
        root.setText("root " + i);
        TreeItem item = root;
        for (int j = 0; j < 99; j++) {
            item = new TreeItem(item, SWT.NONE);
            item.setText("descendent " + j);
        }
        root.setExpanded(true);
    }
    dbTree.setLinesVisible(true);
    //TreeViewer fDBTreeViewer = new TreeViewer(dbTree);
    FormData formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.top = new FormAttachment(0, 0);
//  formData.bottom = new FormAttachment(100, 0);
    final int NUM_ROWS = 10;
//  FontMetrics fontMetrics =
shell.getFont().getFontData().FigureUtilities.getFontMetrics(parent.getFont(
));
    formData.height = /*fontMetrics.getHeight()*/dbTree.getItemHeight() *
NUM_ROWS;
    dbTree.setLayoutData(formData);

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
}

HTH,
Grant


"Matt Dickie" <m.dickie@xxxxxxxxxx> wrote in message
news:h8lcte$kfq$1@xxxxxxxxxxxxxxxxxxxx
> Hi all,
>
> I am having difficult limiting the height of a tree control with
> FormLayout. The code is below:
>
>      Tree dbTree = getWidgetFactory().createTree(composite, SWT.SINGLE |
> SWT.V_SCROLL);
>      fDBTreeViewer = new TreeViewer(dbTree);
>      formData = new FormData();
>      formData.left = new FormAttachment(0, 0);
>      formData.right = new FormAttachment(100, 0);
>      formData.top = new FormAttachment(0, 0);
> //    formData.bottom = new FormAttachment(100, 0);
>      final int NUM_ROWS = 10;
>      FontMetrics fontMetrics =
> FigureUtilities.getFontMetrics(parent.getFont());
>      formData.height = fontMetrics.getHeight() * NUM_ROWS;
>      dbTree.setLayoutData(formData);
>
> I set the auto expand for the viewer to 2. If there are many items at
> this level then the tree control becomes very tall and looks ridiculous.
> (The tree control is part of a section within a TabbedPropertySheet).
>
> As you can see I do set the height of the formData to 10 * font height,
> but that doesn't seem to do anything. If I don't set auto expand to 2,
> then with just 1 root element, the tree control is ridiculously short,
> perhaps only 2 rows tall. Uncommenting out the line where
> formData.bottom is set appears to make little difference either.
>
> How can I set the height of the tree control to consistently be 10 rows
> tall?
>
> Thanks in advance,
>
> Matt D.