Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Custom focusable component(How can I make custom focusable component?)
Custom focusable component [message #950551] Sat, 20 October 2012 00:51 Go to next message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 11
Registered: July 2009
Junior Member
I have following widget layout:
Composite
|MyCustomPanel
||Label
||Button
|MyCustomPanel
||Label
||Button

"MyCustomPanel" is a custom class subclassing Composite.

What I need is the focus cycling between "MyCustomPanel" instances when the user presses tab - e.g. panels must get the focus events and buttons should not get the focus. Currently (I'm on GTK) focus is aquired by buttons.

Is there a way to do it in SWT?
Re: Custom focusable component [message #966053 is a reply to message #950551] Wed, 31 October 2012 18:13 Go to previous message
Grant Gayed is currently offline Grant Gayed
Messages: 2151
Registered: July 2009
Senior Member
Hi, sorry for the late reply,

I think the following snippet demonstrates what you need. It has a
Composite (with child Button) that can receive focus, and handles its
own traversals. The only case the snippet does not address is if focus
is in the control following the Composite and a user Ctrl+Tab's
backwards into the Composite, which will leave focus in its child
button. To handle this I think the Composite has to add FocusIn
listeners to all of its children, and take the focus for itself when
this happens.

public class ModifiedSnippet21 {
public static void main (String [] args) {
Display display = new Display ();
final Color red = display.getSystemColor (SWT.COLOR_RED);
final Color blue = display.getSystemColor (SWT.COLOR_BLUE);
Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
final Button defaultButton = new Button (shell, SWT.PUSH);
defaultButton.setText ("Default Button");
shell.setDefaultButton (defaultButton);
defaultButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("default button selection");
}
});
defaultButton.addListener (SWT.FocusIn, new Listener () {
public void handleEvent (Event e) {
System.out.println("defaultButton focus in");
}
});
defaultButton.addListener (SWT.FocusOut, new Listener () {
public void handleEvent (Event e) {
System.out.println("defaultButton focus out");
}
});
final Composite composite = new Composite (shell, SWT.BORDER);
composite.setLayout(new GridLayout());
Button childButton = new Button(composite, SWT.PUSH);
childButton.setText("child button");
composite.addListener (SWT.FocusIn, new Listener () {
public void handleEvent (Event e) {
System.out.println("composite focus in");
composite.setBackground (red);
}
});
composite.addListener (SWT.FocusOut, new Listener () {
public void handleEvent (Event e) {
System.out.println("composite focus out");
composite.setBackground (blue);
}
});
composite.addListener (SWT.KeyDown, new Listener () {
public void handleEvent (Event e) {
System.out.println ("keypress in composite");
}
});
final Text text = new Text (shell, SWT.SINGLE | SWT.BORDER);
composite.addListener (SWT.Traverse, new Listener () {
public void handleEvent (Event e) {
switch (e.detail) {
/* Do tab group traversal */
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
case SWT.TRAVERSE_TAB_PREVIOUS:
e.doit = true;
break;
case SWT.TRAVERSE_TAB_NEXT:
/* custom traversal, skips the child button */
e.doit = false;
text.setFocus();
break;
}
}
});
composite.setFocus ();
shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

HTH,
Grant


On 10/19/2012 8:51 PM, Eugene Ostroukhov wrote:
> I have following widget layout:
> Composite
> |MyCustomPanel
> ||Label
> ||Button
> |MyCustomPanel
> ||Label
> ||Button
>
> "MyCustomPanel" is a custom class subclassing Composite.
>
> What I need is the focus cycling between "MyCustomPanel" instances when
> the user presses tab - e.g. panels must get the focus events and buttons
> should not get the focus. Currently (I'm on GTK) focus is aquired by
> buttons.
>
> Is there a way to do it in SWT?
Previous Topic:SWT 3.7 native source code where?
Next Topic:SWT 4.2.1 and Windows 8
Goto Forum:
  


Current Time: Thu Oct 23 05:57:14 GMT 2014

Powered by FUDForum. Page generated in 0.02801 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software