问题
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
- CustomPanel1
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 Button
s one
and three
ignoring the Button
s 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 Composite
s 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