如何在复合控件内的单选按钮分组时将焦点设置在复选框上

时间:2013-06-05 17:51:15

标签: java eclipse-plugin swt

我创建了具有复选框和两个单选按钮的复合控件。这类似于使用复选框控制单选按钮,即只有在选中复选框后才会启用无线电

我希望在页面出现时将焦点设置在复选框上(使用CTRl + TAB进行复合遍历),在没有启用任何单选按钮的情况下,它可以正常工作。

如果选择了任何单选按钮,则焦点将转到该单选按钮,但不会转到已选中框。这是Windows默认行为,焦点应该是选中的单选按钮? 我怎样才能使焦点始终在复选框上? 我试图在复选框上使用setFocus,但这没有帮助。

所有这三个按钮都在一组中。

以下是示例代码

    Composite composite = this;

    GridData LData = new GridData();
    LData.horizontalSpan = 1;
    LData.horizontalAlignment = GridData.FILL;
    LData.grabExcessHorizontalSpace = true;
    LData.verticalAlignment = GridData.FILL;
    LData.grabExcessVerticalSpace = true;
    composite.setLayoutData(LData);
    composite.setLayout(new GridLayout(1, false));

    Label infoLabel = new Label(composite, SWT.NONE);
    infoLabel.setText("Test");  

    rGroup = new Group(composite, SWT.NONE);    
    GridData rGroupLData = new GridData();
    rGroupLData.grabExcessHorizontalSpace = true;
    rGroupLData.horizontalAlignment = GridData.FILL;
    rGroup.setLayoutData(rGroupLData);
    GridLayout rGroupLayout = new GridLayout(5, false);
    rGroup.setLayout(rGroupLayout);

    checkBox1 = new Button(rGroup, SWT.CHECK);
    checkBox1.setText("CheckBox");
    GridData rcheckBox1LData = new GridData();
    rcheckBox1LData.horizontalSpan = 5;
    rcheckBox1LData.verticalAlignment = GridData.CENTER;
    rcheckBox1LData.grabExcessHorizontalSpace = true;
    rcheckBox1LData.horizontalAlignment = GridData.FILL;
    checkBox1.setLayoutData(rcheckBox1LData);

    GridData r1LData  = new GridData();
    r1LData.horizontalAlignment = GridData.CENTER;
    r1LData.grabExcessHorizontalSpace = true;
    r1LData.verticalAlignment = GridData.CENTER;

    r1Numeric = new Button(rGroup, SWT.RADIO);
    r1Numeric.setText("Radio1");
    r1Numeric.setSelection(true);
    r1Numeric.setLayoutData(r1LData);
    r1Numeric.setEnabled(false);        

    GridData selR1LData = new GridData();
    selR1LData.horizontalAlignment = GridData.CENTER;
    selR1LData.grabExcessHorizontalSpace = true;
    selR1LData.verticalAlignment = GridData.CENTER;

    selR1Value = new Button(rGroup, SWT.NONE);
    selR1Value
            .setText("Select R1");
    selR1Value.setLayoutData(selR1LData);
    selR1Value.setEnabled(false);



    GridData r2LData = new GridData();
    r2LData.horizontalAlignment = GridData.CENTER;
    r2LData.grabExcessHorizontalSpace = true;
    r2LData.verticalAlignment = GridData.CENTER;        

    r2Numeric = new Button(rGroup, SWT.RADIO);
    r2Numeric.setText("Radio2");
    r2Numeric.setLayoutData(r2LData);
    r2Numeric.setSelection(false);
    r2Numeric.setEnabled(false);

    GridData selR2LData = new GridData();
    selR2LData.horizontalAlignment = GridData.CENTER;
    selR2LData.grabExcessHorizontalSpace = true;
    selR2LData.verticalAlignment = GridData.CENTER;

    selR2Value = new Button(rGroup, SWT.NONE);
    selR2Value
            .setText("Select R2");
    selR2Value.setLayoutData(selR2LData);
    selR2Value.setEnabled(false);

1 个答案:

答案 0 :(得分:0)

鉴于所提供的代码与您的机器上的代码不完全相同,我无法重现您的问题,但在这里我可以遍历所有组件。

无论如何,您可以设置组件的Tab键顺序。根据您的示例,尝试在创建所有内容后添加此代码:

rGroup.setTabList( new Control[] { checkBox1, r1Numeric, selR1Value, r2Numeric, selR2Value } );

除此之外,您还可以实现页面的setFocus方法(如果存在),使其集中在复选框上。