Components won't resize in SWT with a GridLayout

谁都会走 提交于 2020-01-17 06:06:56

问题


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).

P.S : 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.


回答1:


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.



来源:https://stackoverflow.com/questions/37442262/components-wont-resize-in-swt-with-a-gridlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!