Java Tutorial/SWT/FocusEvent
Prevent Tab from traversing out of a control
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ControlTabTransversing {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Text text1 = new Text(shell, SWT.SINGLE|SWT.BORDER);
text1.setText("Can"t Traverse");
text1.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
switch (e.detail) {
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS: {
e.doit = false;
}
}
}
});
Text text2 = new Text(shell, SWT.SINGLE|SWT.BORDER);
text2.setText("Can Traverse");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Set Tab list for focus transfer
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TabListFocusTransfer {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Button b1 = new Button(shell, SWT.PUSH);
b1.setText("1");
Button b2 = new Button(shell, SWT.RADIO);
b2.setText("2");
Button b3 = new Button(shell, SWT.RADIO);
b3.setText("3");
Button b4 = new Button(shell, SWT.RADIO);
b4.setText("4");
Button b5 = new Button(shell, SWT.PUSH);
b5.setText("5");
Control[] tabList1 = new Control[] { b2, b1, b3, b5, b4 };
shell.setTabList(tabList1);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}