SWT - 从Shell中的按钮/控件中移除焦点

时间:2015-09-29 09:02:02

标签: java button focus swt

我有一个包含几个按钮的窗口(窗口充当起始页面)。首次创建并打开using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string input = "sdjkgjkdgjk<br />asdfsdg"; string pattern = "<br.*\\/>"; // Split on <br/> DisplayByRegex(input, pattern); input = "sdjkgjkdgjk<br style=\"someAttribute: someProperty;\"/>asdfsdg"; DisplayByRegex(input, pattern); Console.Read(); } private static void DisplayByRegex(string input, string pattern) { string[] substrings = Regex.Split(input, pattern); foreach (string match in substrings) { Console.WriteLine("'{0}'", match); } } } 时,会为创建的第一个Shell分配焦点。有没有办法从此Button删除焦点?

使用Button无济于事。

2 个答案:

答案 0 :(得分:3)

而不是创建虚拟/不必要的按钮你可以只关注你的shell:

shell.open();
shell.forceFocus();

至少在gtk中这样做。

答案 1 :(得分:0)

最简单的解决方法可能是使用以下步骤:

  1. 创建一个无法执行任何操作的虚拟按钮
  2. 将焦点指定给该按钮
  3. 隐藏它(将其定位以便无法看到)
  4. 如果您使用的是Button dummy = new Button(parent, SWT.PUSH) dummy.setFocus() FormData data = new FormData(); data.bottom = new FormAttachment(-1); dummy.setLayoutData(data);

    ,则最容易做到这一点
    Composite

    这假设父clientArea的{​​{1}}占据整个窗口,或位于顶部。

    完整的工作示例

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.layout.FormAttachment;
    import org.eclipse.swt.layout.FormData;
    import org.eclipse.swt.layout.FormLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    
    public class SOClass {
    
        public static void main(String[] args) {
            Display d = new Display();
            Shell shell = new Shell(d);
            shell.setSize(500, 500);
            shell.setText("Remove focus from first button");
    
            shell.setLayout(new FillLayout());
    
            Composite comp = new Composite(shell, SWT.NONE);
            comp.setLayout(new FormLayout());
    
            int style = SWT.PUSH | SWT.BORDER;
            Button b1 = new Button(comp, style);
            Button b2 = new Button(comp, style);
    
            b1.setText("Button 1");
            //position: top, spanning the majority of the windows width
            FormData b1Data = new FormData();
            b1Data.top = new FormAttachment(0, 5);
            b1Data.left = new FormAttachment(0, 5);
            b1Data.right = new FormAttachment(100, -5);
            b1.setLayoutData(b1Data);
    
            b2.setText("Button 2");
            //position: under b1, spanning the majority of the windows width
            FormData b2Data = new FormData();
            b2Data.top = new FormAttachment(b1, 5);
            b2Data.left = new FormAttachment(0, 5);
            b2Data.right = new FormAttachment(100, -5);
            b2.setLayoutData(b2Data);
    
            //try removing/commenting this section to see the effect
            Button dummy = new Button(comp, style);
            dummy.setFocus();
            FormData dummyData = new FormData();
            dummyData.bottom = new FormAttachment(-1);
            dummy.setLayoutData(dummyData);
    
            shell.open();
    
            while (!shell.isDisposed()) {
                if(!d.readAndDispatch())
                    d.sleep();
            }
            d.dispose();
        }
    
    }
    

    为什么这样做?

    虚拟按钮仍然可用,您仍然可以单击它(物理和编程),但它不可见,因此对于用户来说,它看起来似乎没有为按钮指定焦点。

    Button.setVisible(false)不起作用 - SWT将按钮识别为隐藏,因此SWT会查找下一个按钮以获得焦点。

    如果您需要再次关注任何按钮,只需使用dummy.setFocus()

    即可