我创建了一个OK按钮(按钮)。
根据用户输入,我想动态创建1到10个SWT按钮(复选框)。
如何创建?
如果单击“确定”按钮,是否已选中如何显示所有复选框按钮?
请查看我正在尝试的以下代码段:
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?
}
}
答案 0 :(得分:4)
这将打印出按钮的选择状态:
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());
}
}
}