i am thinking how is it possible to generate dynamically the modules along with text and button In the image there are three modules but there might be even five or six modules. Please help to populate number of modules along with the text and browse button next to them
1 Answer
Just create the control from a list.
// Just using a 'record' here to represent the data to be display
// Could also be a normal class
record Module(String check, String path)
{ }
...
Composite body = .... // Parent composite from somewhere
body.setLayout(new GridLayout(3, false));
// An example list of modules
List<Module> modules = List.of(new Module("Port", "Path 1"), new Module("PWM", "Path 2"), new Module("CAN", "Path 3"));
modules.forEach(module -> {
Button checkButton = new Button(body, SWT.CHECK);
checkButton.setText(module.check());
Text pathText = new Text(body, SWT.BORDER | SWT.READ_ONLY);
pathText.setText(module.path());
pathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button browseButton = new Button(body, SWT.PUSH);
browseButton.setText("Browse");
browseButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(event -> {
// Example action to take when Browse is pressed
System.out.println("Browse " + module.path());
}));
});
If you want to keep changing the list in the same dialog you can dispose the existing children and redo the Composite layout. The following method does that:
void showModules(final Composite body, final List<Module> modules)
{
// Dispose any existing children
Arrays.stream(body.getChildren()).forEach(Control::dispose);
modules.forEach(module -> {
Button checkButton = new Button(body, SWT.CHECK);
checkButton.setText(module.check());
Text pathText = new Text(body, SWT.BORDER | SWT.READ_ONLY);
pathText.setText(module.path());
pathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button browseButton = new Button(body, SWT.PUSH);
browseButton.setText("Browse");
browseButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(event -> {
System.out.println("Browse " + module.path());
}));
});
// Redo Composite layout
body.layout(true, true);
}
