Chris Clark
Chris Clark

Reputation: 340

Eclipse SWT Text Boxes change size

I had an Eclipse plug-in app (ten-year-old code, no documentation, etc.) dropped in my lap and while adding new features to it, I noticed that when a panel is resized, the text boxes change size continuously while the separator is being dragged.This is what it should look like

After resizing

As you can see in the second picture, the text boxes are kind of randomly sized. Is there a setting in SWT that will prevent this from happening?

Here's how I'm creating one of the text boxes. The others are basically clones of this:

    Font font = parent.getFont();
    setLayout(new FillLayout());

    SashForm sashForm = new SashForm(this, SWT.VERTICAL);

    FormToolkit toolkit = new FormToolkit(getParent().getDisplay());
    Section section = toolkit.createSection(sashForm,
            Section.DESCRIPTION | ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
    section.setLayoutData(new GridData(GridData.FILL_BOTH));
    section.setLayout(new GridLayout());
    section.setText("Section Title");
    Composite controlComposite = toolkit.createComposite(section);
    GridLayout controlLayout = new GridLayout();
    controlLayout.numColumns = 2;
    controlLayout.verticalSpacing = 20;
    controlComposite.setLayout(controlLayout);
    section.setClient(controlComposite);

    Font bold = ResourceManager.getBoldFont(font);

    Label textLabel = toolkit.createLabel(controlComposite, "Title:", SWT.BOLD);
    GridData gd = new GridData();
    gd.horizontalSpan = 1;
    textLabel.setLayoutData(gd);
    textLabel.setFont(bold);

    textBox = new ExtendedText(controlComposite, SWT.BORDER | SWT.SINGLE, false);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 1;
    gd.verticalSpan = 2;
    textBox.setLayoutData(gd);

The ExtendedText class is an extension of StyledText. The important bits of it are this:

    GridData gd_bg = new GridData(GridData.FILL_BOTH);
    setLayoutData(gd_bg);

    final GridLayout gridLayout = new GridLayout();
    gridLayout.verticalSpacing = 0;
    gridLayout.marginWidth = 0;
    gridLayout.marginHeight = 0;
    gridLayout.horizontalSpacing = 0;
    gridLayout.numColumns = 1;
    gridLayout.makeColumnsEqualWidth = true;

    sashForm.setWeights(new int[] { 1, 1 });

Upvotes: 0

Views: 976

Answers (1)

Chris Clark
Chris Clark

Reputation: 340

Okay, after digging in a little deeper, I got it working as expected.

First, the controlComposite and controlLayout objects are now created using

Composite controlComposite = new Composite(section, SWT.NONE)
controlComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
controlComposite.setBackground(section.getBackground());
GridLayout controlLayout = new GridLayout(2, true);
controlLayout.marginHeight = 20;
controlLayout.marginWidth = 0;
controlLayout.verticalSpacing = 10;
controlLayout.horizontalSpacing = 0;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);

Once I did that, things started to stabilize. I also ended up tweaking the weights to this:

sashForm.setWeights(new int[] { 2, 3 });

It's not perfect, but it'll do for now.

Thanks to @greg-449 and @Baz for taking a look

Upvotes: 1

Related Questions