单击按钮后防止单选按钮重置

时间:2020-02-06 16:48:19

标签: swt

我有一个自定义对话框,其中包含两个SWT.RADIO按钮和一个SWT.PUSH按钮(打开FileDialog)。单击第二个SWT.RADIO按钮,单击SWT.PUSH按钮并从FileDialog返回后,SWT.RADIO按钮将重置为其默认选择。

如何预防?

可复制的代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class TestDialog {

    public static void main(String[] args) {
        final Display display = new Display();

        Shell shell = new Shell(display);
        shell.setBounds(30, 30, 300, 150);

        shell.setLayout(new GridLayout(1, false));

        Composite comp = new Composite(shell, SWT.NONE);
        comp.setLayout(new GridLayout(2, false));

        Button btnRadio1 = new Button(comp, SWT.RADIO);
        btnRadio1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        btnRadio1.setText("Radio Button 1");

        Button btnRadio2 = new Button(comp, SWT.RADIO);
        btnRadio2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        btnRadio2.setText("Radio Button 2");

        Composite comp2 = new Composite(shell, SWT.NO_RADIO_GROUP);
        comp2.setLayout(new GridLayout(1, false));

        Button btnPush = new Button(comp2, SWT.NONE);
        btnPush.setText("Push Button");
        btnPush.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                FileDialog dirDialog = new FileDialog(shell);
                dirDialog.open();
            }
        });

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

0 个答案:

没有答案