2

I have one OK button(push button) created.

Based on user input i want to dynamically create 1 to 10 SWT buttons(check box).

How to create it?

If OK button is clicked, how to display which are all check box button has been selected?

Please find below snippet I am trying with:

Set<String> Groups = getData(Contents);
for(String group : contentGroups) {

contentButton = new Button(fComposite, SWT.CHECK);  

    // is this right way to create dynamic buttons?

    contentButton.setText(group);

}

okButton = new Button(lowComposite, SWT.PUSH);

okButton.addSelectionListener(new SelectionListener(){

    @Override
    public void widgetSelected(SelectionEvent e){

        //Here how to get the selection status of contentButtons?       
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
yash
  • 101
  • 2
  • 9
  • You will have to save the `Button`s in a `final` or `static` `List` to be able to access them / iterate over them in the `Listener` – Baz Mar 06 '13 at 10:31
  • But how to save and iterate? how to get variable name of all contentButtons?ok button and check boxes are created in different composites. Please give some snippet. – yash Mar 06 '13 at 10:33

1 Answers1

4

This will print out the selection state of the buttons:

Set<String> Groups = getData(Contents);

final List<Button> buttons = new ArrayList<Button>();

for(String group : contentGroups)
{
    Button newButton = new Button(fComposite, SWT.CHECK);  
    newButton.setText(group);

    // save the button
    buttons.add(newButton);
}

Button okButton = new Button(lowComposite, SWT.PUSH);

okButton.addListener(SWT.Selection, new Listener()
{
    @Override
    public void handleEvent(Event e)
    {
        // iterate over saved buttons
        for(Button button : buttons)
        {
            System.out.println(button.getText() + ": " + button.getSelection());
        }       
    }
}
Baz
  • 36,440
  • 11
  • 68
  • 94