Focusable composite in SWT

依然范特西╮ 提交于 2019-12-08 05:17:49

问题


We have a custom control that is essentially a composite with label and a button. Currently when the user presses "Tab" the focus comes to the button.

How can I make the composite to receive focus and the button to be excluded from the focus? E.g. the user should be able to tab through all custom controls and not stop at buttons.

Updated: Our controls tree looks like this:

  • Main pane
    • CustomPanel1
      • Label
      • Button
    • CustomPanel2
      • Label
      • Button
    • CustomPanel3
      • Label
      • Button

All CustomPanel's are of the same Composite subclass. What we need is for the tab to cycle between those panels and do not "see" buttons (those are the only focusable components)


回答1:


You can define the tab order of a Composite by using Composite#setTabList(Control[]).

Here is a small example that will tab between the Buttons one and three ignoring the Buttons two and four:

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    Composite content = new Composite(shell, SWT.NONE);
    content.setLayout(new GridLayout(2, true));
    content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final Button one = new Button(content, SWT.PUSH);
    one.setText("One");

    final Button two = new Button(content, SWT.PUSH);
    two.setText("Two");

    final Button three = new Button(content, SWT.PUSH);
    three.setText("Three");

    final Button four = new Button(content, SWT.PUSH);
    four.setText("Four");

    Control[] controls = new Control[] {one, three};

    content.setTabList(controls);

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

EDIT: The code above can easily be converted to fit your requirements. I can't test it myself, because Composites aren't focus-able, but you should get the idea:

mainPane.setTabList(new Control[] {customPanel1, customPanel2, customPanel3 });

customPanel1.setTabList(new Control[] {});
customPanel2.setTabList(new Control[] {});
customPanel3.setTabList(new Control[] {});


来源:https://stackoverflow.com/questions/12923814/focusable-composite-in-swt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!