Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Shift-Tab + Key Listeners
Shift-Tab + Key Listeners [message #463580] |
Wed, 09 November 2005 16:54 |
Scott Myron Messages: 3 Registered: July 2009 |
Junior Member |
|
|
Hello.
I've experienced a problem with Shift+Tab when working with custom widgets
that extend Composite and that have a key listener (using either addListener
or addKeyListener) attached. For example: Let's say I have a custom widget
and a button. When I start the application, the focus goes to the custom
widget. If I press Tab, focus goes to the button. If I press Tab again, the
focus goes back to the custom widget, as it should.
However, if I press "Shift+Tab", and the focus is in on the custom widget,
the focus does not go back to the button. If I click on the button, then
press Shift+Tab, focus returns to the custom widget, as it should. Of
course, pressing Shift+Tab again does nothing. The traversal event does
fire, though. Attached is a code sample to demonstrate this problem. Any
help would be greatly appreciated.
Also, if I remove the key listener from the custom widget, Shift+Tab works
(lines 104-109 in sample) . This, however, is not a possiblilty because we
need to add key listeners to our widgets.
Thanks,
Scott
|
|
|
Re: Shift-Tab + Key Listeners [message #463602 is a reply to message #463580] |
Thu, 10 November 2005 16:14 |
Steve Northover Messages: 1636 Registered: July 2009 |
Senior Member |
|
|
CAn you paste in the code? My stupid mailer has removed your attachment.
"Scott Myron" <samyron@gmail.com> wrote in message
news:dkt9kf$s7h$1@news.eclipse.org...
> Hello.
>
> I've experienced a problem with Shift+Tab when working with custom widgets
> that extend Composite and that have a key listener (using either
addListener
> or addKeyListener) attached. For example: Let's say I have a custom widget
> and a button. When I start the application, the focus goes to the custom
> widget. If I press Tab, focus goes to the button. If I press Tab again,
the
> focus goes back to the custom widget, as it should.
>
> However, if I press "Shift+Tab", and the focus is in on the custom widget,
> the focus does not go back to the button. If I click on the button, then
> press Shift+Tab, focus returns to the custom widget, as it should. Of
> course, pressing Shift+Tab again does nothing. The traversal event does
> fire, though. Attached is a code sample to demonstrate this problem. Any
> help would be greatly appreciated.
>
> Also, if I remove the key listener from the custom widget, Shift+Tab works
> (lines 104-109 in sample) . This, however, is not a possiblilty because we
> need to add key listeners to our widgets.
>
> Thanks,
>
> Scott
>
>
>
|
|
|
Re: Shift-Tab + Key Listeners [message #463603 is a reply to message #463602] |
Thu, 10 November 2005 16:24 |
Scott Myron Messages: 3 Registered: July 2009 |
Junior Member |
|
|
Sure. Here is the code:
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
class ExtendedText extends Composite
{
private Composite parent;
private Text text;
private Listener defaultListener = new Listener() {
public void handleEvent(Event event)
{
switch(event.type) {
case SWT.KeyUp:
case SWT.KeyDown:
Event newEvent = new Event();
newEvent.type = event.type;
newEvent.keyCode = event.keyCode;
newEvent.character = event.character;
newEvent.stateMask = event.stateMask;
newEvent.doit = event.doit;
ExtendedText.this.notifyListeners(event.type, newEvent);
break;
case SWT.Traverse:
event.doit = true;
ExtendedText.this.notifyListeners(event.type, event);
break;
}
}
};
public ExtendedText(Composite parent, int style)
{
super(parent, SWT.NONE);
this.parent = parent;
setLayout(new FillLayout());
text = new Text(this, style);
text.addListener(SWT.KeyDown, defaultListener);
text.addListener(SWT.KeyUp, defaultListener);
text.addListener(SWT.Traverse, defaultListener);
}
public String getText()
{
return text.getText();
}
public void setText(String str)
{
text.setText(str);
}
public boolean forceFocus()
{
return text.forceFocus();
}
public boolean setFocus()
{
return text.setFocus();
}
}
public class TestTabBug
{
private Display display;
private Shell shell;
public TestTabBug()
{
display = new Display();
shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.HORIZONTAL));
createControls();
}
public void run()
{
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
public void createControls()
{
ExtendedText text = new ExtendedText(shell, SWT.BORDER);
text.addListener(SWT.KeyDown, new Listener() {
public void handleEvent(Event event)
{
System.out.println("Key Down: " + event);
}
});
text.addListener(SWT.Traverse, new Listener(){
public void handleEvent(Event event)
{
System.out.println("Traverse event in parent: " + event);
event.doit = true;
}
});
Button button = new Button(shell, SWT.PUSH);
button.setText("Push me");
}
public static void main(String[] args)
{
new TestTabBug().run();
}
}
"Steve Northover" <steve_northover@ca.ibm.com> wrote in message
news:dkvrln$b1b$1@news.eclipse.org...
> CAn you paste in the code? My stupid mailer has removed your attachment.
>
> "Scott Myron" <samyron@gmail.com> wrote in message
> news:dkt9kf$s7h$1@news.eclipse.org...
>> Hello.
>>
>> I've experienced a problem with Shift+Tab when working with custom
>> widgets
>> that extend Composite and that have a key listener (using either
> addListener
>> or addKeyListener) attached. For example: Let's say I have a custom
>> widget
>> and a button. When I start the application, the focus goes to the custom
>> widget. If I press Tab, focus goes to the button. If I press Tab again,
> the
>> focus goes back to the custom widget, as it should.
>>
>> However, if I press "Shift+Tab", and the focus is in on the custom
>> widget,
>> the focus does not go back to the button. If I click on the button, then
>> press Shift+Tab, focus returns to the custom widget, as it should. Of
>> course, pressing Shift+Tab again does nothing. The traversal event does
>> fire, though. Attached is a code sample to demonstrate this problem. Any
>> help would be greatly appreciated.
>>
>> Also, if I remove the key listener from the custom widget, Shift+Tab
>> works
>> (lines 104-109 in sample) . This, however, is not a possiblilty because
>> we
>> need to add key listeners to our widgets.
>>
>> Thanks,
>>
>> Scott
>>
>>
>>
>
>
|
|
|
Re: Shift-Tab + Key Listeners [message #463611 is a reply to message #463580] |
Thu, 10 November 2005 17:37 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
What is happening is that by adding a key listener to ExtendedText, it is
assumed that ExtendedText can take keyboard focus and it is therefore
included in the tab order. When you Shift+Tab, you are going backwards in
tab order and the ExtendedText composite is the previous guy in the tab
order so it is taking focus. However, you overrode setFocus() so focus is
being forced back to the Text widget inside.
I have modified your code below so that it will work. Note that this is
required because Text is the first child of ExtendedText. If you have
multiple children, you would only need this for the first child.
class ExtendedText extends Composite {
private Composite parent;
private Text text;
private Listener defaultListener = new Listener() {
public void handleEvent(Event event)
{
switch(event.type) {
case SWT.KeyUp:
case SWT.KeyDown:
Event newEvent = new Event();
newEvent.type = event.type;
newEvent.keyCode = event.keyCode;
newEvent.character = event.character;
newEvent.stateMask = event.stateMask;
newEvent.doit = event.doit;
ExtendedText.this.notifyListeners(event.type,
newEvent);
break;
case SWT.Traverse:
event.doit = true;
ExtendedText.this.notifyListeners(event.type,
event);
break;
}
}
};
public ExtendedText(Composite parent, int style)
{
super(parent, SWT.NONE);
this.parent = parent;
setLayout(new FillLayout());
text = new Text(this, style);
text.addListener(SWT.KeyDown, defaultListener);
text.addListener(SWT.KeyUp, defaultListener);
text.addListener(SWT.Traverse, defaultListener);
text.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event event) {
if (event.detail ==
SWT.TRAVERSE_TAB_PREVIOUS) {
ExtendedText.this.traverse(event.detail);
event.detail = SWT.TRAVERSE_NONE;
event.doit = true;
}
}
});
this.addListener(SWT.FocusIn, new Listener() {
public void handleEvent(Event e) {
text.setFocus();
}
});
}
public String getText()
{
return text.getText();
}
public void setText(String str)
{
text.setText(str);
}
// DO NOT override these guys
// public boolean forceFocus()
// {
// return text.forceFocus();
// }
//
// public boolean setFocus()
// {
// return text.setFocus();
// }
}
|
|
|
Re: Shift-Tab + Key Listeners [message #463617 is a reply to message #463611] |
Thu, 10 November 2005 19:47 |
Scott Myron Messages: 3 Registered: July 2009 |
Junior Member |
|
|
Thank you very much!
Scott
"Veronika Irvine" <veronika_irvine@oti.com> wrote in message
news:dl00fu$jjj$1@news.eclipse.org...
> What is happening is that by adding a key listener to ExtendedText, it is
> assumed that ExtendedText can take keyboard focus and it is therefore
> included in the tab order. When you Shift+Tab, you are going backwards in
> tab order and the ExtendedText composite is the previous guy in the tab
> order so it is taking focus. However, you overrode setFocus() so focus is
> being forced back to the Text widget inside.
>
> I have modified your code below so that it will work. Note that this is
> required because Text is the first child of ExtendedText. If you have
> multiple children, you would only need this for the first child.
>
> class ExtendedText extends Composite {
> private Composite parent;
> private Text text;
>
> private Listener defaultListener = new Listener() {
> public void handleEvent(Event event)
> {
> switch(event.type) {
> case SWT.KeyUp:
> case SWT.KeyDown:
> Event newEvent = new Event();
> newEvent.type = event.type;
> newEvent.keyCode = event.keyCode;
> newEvent.character = event.character;
> newEvent.stateMask = event.stateMask;
> newEvent.doit = event.doit;
>
> ExtendedText.this.notifyListeners(event.type, newEvent);
> break;
> case SWT.Traverse:
> event.doit = true;
>
> ExtendedText.this.notifyListeners(event.type, event);
> break;
> }
> }
> };
>
> public ExtendedText(Composite parent, int style)
> {
> super(parent, SWT.NONE);
> this.parent = parent;
> setLayout(new FillLayout());
> text = new Text(this, style);
> text.addListener(SWT.KeyDown, defaultListener);
> text.addListener(SWT.KeyUp, defaultListener);
> text.addListener(SWT.Traverse, defaultListener);
>
> text.addListener(SWT.Traverse, new Listener() {
> public void handleEvent(Event event) {
> if (event.detail ==
> SWT.TRAVERSE_TAB_PREVIOUS) {
>
> ExtendedText.this.traverse(event.detail);
> event.detail = SWT.TRAVERSE_NONE;
> event.doit = true;
> }
> }
> });
> this.addListener(SWT.FocusIn, new Listener() {
> public void handleEvent(Event e) {
> text.setFocus();
> }
> });
> }
>
> public String getText()
> {
> return text.getText();
> }
>
> public void setText(String str)
> {
> text.setText(str);
> }
>
> // DO NOT override these guys
> // public boolean forceFocus()
> // {
> // return text.forceFocus();
> // }
> //
> // public boolean setFocus()
> // {
> // return text.setFocus();
> // }
> }
>
|
|
|
Goto Forum:
Current Time: Fri Apr 26 13:55:41 GMT 2024
Powered by FUDForum. Page generated in 0.03185 seconds
|