简体   繁体   中英

Components won't resize in SWT with a GridLayout

I discovered SWT a few days ago and decided to switch my plugin interface from Swing to SWT. I can place the components as I want, but when I resize the window, the components are not resized at all. Furthermore, when I fill a little Text (text area) with a large string, I can't find a way to resize it... The following code is the one defining the layout and the components, I guess someone will find where my mistake(s) is(are).

PS : I see some tutorials online declaring a Display object before declaring the shell. When I do, I encounter an InvalidThreadAccess Exception.

    Shell shell = new Shell();
    GridLayout gridLayout = new GridLayout(2, false);
    shell.setLayout(gridLayout);

    tree = new Tree(shell, SWT.CHECK | SWT.BORDER);

    Text tips = new Text(shell, SWT.READ_ONLY);
    tips.setText("Pick the files and nodes to refactor : ");
    oldFileViewer = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    oldFileViewer.setText("here is the old file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
    oldFileViewer.setSize(400, 400);

    newFileViewer = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    newFileViewer.setText("and here is the new file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
    newFileViewer.setSize(400, 400);
    Button ok = new Button(shell, SWT.PUSH);

Thanks for reading.

Don't try and mix Layouts with setSize and setBounds it won't work.

Using layouts your code might look like:

GridLayout gridLayout = new GridLayout(2, false);
shell.setLayout(gridLayout);

tree = new Tree(shell, SWT.CHECK | SWT.BORDER);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
tree.setLayoutData(data);

Text tips = new Text(shell, SWT.READ_ONLY);
tips.setText("Pick the files and nodes to refactor : ");
tips.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

oldFileViewer = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
oldFileViewer.setText("here is the old file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
data = new GridData(SWT.FILL, SWT.FILL, false, false);
data.heightHint = 400;
oldFileViewer.setLayoutData(data);

newFileViewer = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
newFileViewer.setText("and here is the new file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
data = new GridData(SWT.FILL, SWT.FILL, false, false);
data.heightHint = 400;
newFileViewer.setLayoutData(data);

Button ok = new Button(shell, SWT.PUSH);
ok.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

I have called setLayoutData on each control providing a GridData that GridLayout will use to decide how to layout the controls.

Note: If you are writing an Eclipse plugin you never call new Display() - that is only used when writing a standalone SWT program. Eclipse has already created a display.

Rather than just creating a new Shell you may want to look at using the JFace Dialog class which does a lot of the basic dialog handling for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM