gogobebe2 gogobebe2 - 15 days ago 10
Java Question

When I change the text for the button in SWT, the whole button size changes. How can I disable this?

I'm creating a piano program. But when I change the button text to nothing, the size of the button changes. How do I disable automatic resizing? I'm very new to windowbuilder. I've been googling for a while and can't find anything similar to my question.

package com.gmail.gogobebe2.piano;

import org.eclipse.swt.widgets.Composite;

public class Piano extends Composite {

/**
* Create the composite.
* @param parent
* @param style
*/
public Piano(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(9, false));
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);
new Label(this, SWT.NONE);

Button btnNewButton = new Button(this, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
}
});
GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_btnNewButton.heightHint = 261;
btnNewButton.setLayoutData(gd_btnNewButton);

Button button_1 = new Button(this, SWT.NONE);
button_1.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));

Button button = new Button(this, SWT.NONE);
button.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
button.setText("New Button");

Button button_2 = new Button(this, SWT.NONE);
button_2.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
button_2.setText("New Button");

Button button_3 = new Button(this, SWT.NONE);
button_3.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
button_3.setText("New Button");

Button button_4 = new Button(this, SWT.NONE);
button_4.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
button_4.setText("New Button");

Button button_5 = new Button(this, SWT.NONE);
button_5.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
button_5.setText("New Button");

Button button_6 = new Button(this, SWT.NONE);
button_6.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
button_6.setText("New Button");

}

@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}

}


Here are some screenshots of before changed the button text and after I changed the button text

Before

Before

After - changed 2 buttons text to be nothing
After

Answer

To set the width of the buttons you can set the widthHint property of the GridData. In your example, for the first button you can put: gd_btnNewButton.widthHint = 100;

For the other buttons you can change the lines of type:

button_1.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));

with:

GridData gd = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
gd.widthHint = 100;
button_1.setLayoutData(gd);