Java – how to correctly avoid SWT table enlargement?

I have a simple SWT program as follows:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1,false));
    shell.setMinimumSize(300,300);

    // table
    final Table table = new Table(shell,SWT.BORDER);
    final GridData gridData = new GridData(SWT.FILL,SWT.FILL,true,true);
    gridData.heightHint = 0; // "hack"
    table.setLayoutData(gridData);

    // example data
    for (int i = 0; i < 50; i++) {
        final TableItem item = new TableItem(table,SWT.NONE);
        item.setText("item no." + i);
    }

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

What I expect - I want the table to completely fill the shell space without changing its size (screenshots 1 and 3)

Problem - if I add rows to a table, the table automatically resizes (as does the shell) (screenshot 2)

Solution – to avoid this behavior, I added a row of griddata heightHint = 0; To code But it seems like a hacker to me

Question – what is the right way to avoid expanding tables (and shells) when adding data?

Greetings, winklerr

Screenshot 1

Without data, the table and shell are not resized in both versions and behave correctly

Screenshot 2

With the expansion of data, tables and shells, the wrong behavior is only without hackers

Screenshot 3

For data, tables and shells will not resize, add scroll bars, correct behavior, only hackers

Solution

Call the shell at some point before adding data to the table setSize(int width,int height)?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>