I want to display a table with 5 columns and 9 rows. The table will display 45 buttons, each of the buttons will have the number written in the button text. The first column has the numbers from 1 to 9 so the buttons have a width smaller than the next four columns, because the next columns has numbers with two digits.
The objective is to have the same width in the 45 buttons of the five RowLayout.
Composite numbersComposite = new Composite(composite, SWT.NONE);
numbersComposite.setLayout(new GridLayout(5, true));
int rows = 9;
int columns = 5;
for (int i=0; i<columns; i++) {
Composite column = new Composite(numbersComposite, SWT.NONE);
RowLayout columnLayout = new RowLayout(SWT.VERTICAL);
columnLayout.fill = true;
column.setLayout(columnLayout);
for (int j=0;j<rows; j++) {
button = new Button(column, SWT.TOGGLE);
button.setText(""+number);
buttons.add(button);
number++;
}
}
I don't want to set a fixed size to all the buttons because if the user increases the font size in the computer then the text will not be fully visible in the fixed size.
This uses GridLayout
for everything and sets GridData
on all controls and composites to fill the columns:
for (int i = 0; i < columns; i++) {
Composite column = new Composite(numbersComposite, SWT.NONE);
column.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
GridLayout columnLayout = new GridLayout();
column.setLayout(columnLayout);
for (int j = 0; j < rows; j++) {
Button button = new Button(column, SWT.TOGGLE);
button.setText(Integer.toString(number));
button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
buttons.add(button);
number++;
}
}