Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » TRAVERSE_RETURN problem
TRAVERSE_RETURN problem [message #437790] Thu, 10 June 2004 14:55 Go to next message
Sergiy Uvarov is currently offline Sergiy UvarovFriend
Messages: 13
Registered: July 2009
Junior Member
Hi, All.

I have a dialog with a table and two buttons (Cancel and Ok).
I have set Ok button as a default button.

I want that when I select a table item by double click then ok's button
selection listener is called.
To do this I have added to table MouseDoubleClick listener
and do that:
traverse(SWT.TRAVERSE_RETURN) to a dialog.

But instead of ok button cancel selection listener is called.

What have I done wrong?

Here is code:

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;

public class DialogProblem {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout(SWT.VERTICAL));

Button button = new Button(shell, SWT.PUSH);
button.setText("Press");

final Label label = new Label(shell, 0);

button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
label.setText("Pressed: " + (new DialogShell(shell)).getButton());
}
});


shell.setSize(200, 200);
shell.open();

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

}
}


class DialogShell extends Shell implements Listener {
Button ok, cancel;
Composite main;
int BORDER_OFFSET = 10;
boolean opened = false;
String pressedButton;
Table table;

DialogShell(Shell parent) {
super(parent, SWT.SHELL_TRIM);
createButtons();
main.setLayout(new FillLayout(SWT.VERTICAL));

table = new Table(main, SWT.BORDER | SWT.FULL_SELECTION);
table.addListener(SWT.MouseDoubleClick, this);
TableItem item = new TableItem(table, 0);
item.setText("table item, make double click here");
addListener(SWT.Traverse, this);

layout();
setSize(300, 200);
}

public void open() {opened = true; super.open();}

public void close() {super.close(); opened = false;}

public String getButton() {
open();
table.setFocus();
while(opened) { if(!getDisplay().readAndDispatch())
getDisplay().sleep(); }
return pressedButton;
}

private void createButtons() {

// set layout
FormLayout layout = new FormLayout();
setLayout(layout);

// create buttons group
Composite buttonsParent = new Composite(this, 0);
FormData formData = new FormData();
formData.left = new FormAttachment(0, BORDER_OFFSET/2);
formData.right = new FormAttachment(100, -BORDER_OFFSET/2);
formData.bottom = new FormAttachment(100, 0);
buttonsParent.setLayoutData(formData);

GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
buttonsParent.setLayout(gridLayout);

// create separator
Label separator = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL|
SWT.SHADOW_IN);
formData = new FormData();
formData.bottom = new FormAttachment(buttonsParent, -BORDER_OFFSET,
SWT.TOP);
formData.left = new FormAttachment(0, 0);
formData.right = new FormAttachment(100, 0);
separator.setLayoutData(formData);

// create main group
main = new Composite(this, 0);
formData = new FormData();
formData.top = new FormAttachment(0,0);
formData.left = new FormAttachment(0,0);
formData.right = new FormAttachment(100,0);
formData.bottom = new FormAttachment(separator, 0, SWT.TOP);
main.setLayoutData(formData);

Label label = new Label(buttonsParent, 0);
GridData d = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(d);

// create cancel button
cancel = new Button(buttonsParent, SWT.PUSH);
cancel.setText("Cancel");
cancel.addListener(SWT.Selection, this);
cancel.addListener(SWT.Traverse, this);
d = new GridData(GridData.HORIZONTAL_ALIGN_END);
d.widthHint = 80;
cancel.setLayoutData(d);

// create ok button
ok = new Button(buttonsParent, SWT.PUSH);
ok.setText("Ok");
ok.addListener(SWT.Selection, this);
ok.addListener(SWT.Traverse, this);
d = new GridData(GridData.HORIZONTAL_ALIGN_END);
d.widthHint = 80;
ok.setLayoutData(d);
setDefaultButton(ok);
}

public void handleEvent(Event event) {
switch(event.type) {
case SWT.Selection:
close();

if(event.widget == cancel) {
pressedButton = "Cancel";
return;
}

if(event.widget == ok) {
pressedButton = "Ok";
return;
}
break;
case SWT.MouseDoubleClick:
traverse(SWT.TRAVERSE_RETURN);
break;
case SWT.Traverse:
event.doit = true;
if(event.widget == this && event.detail == SWT.TRAVERSE_ESCAPE) {
pressedButton = "Cancel_ESC";
}
break;
}
}
protected void checkSubclass(){}
}
Re: TRAVERSE_RETURN problem [message #438015 is a reply to message #437790] Mon, 14 June 2004 19:51 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Should work although your code is a bit strange. Why not just call the same
code from 2 places instead of using traverse()? Enter a problem report with
the code. Thanks.

"Sergiy Uvarov" <colonel@rts-ukraine.com> wrote in message
news:ca9sbv$ekk$1@eclipse.org...
> Hi, All.
>
> I have a dialog with a table and two buttons (Cancel and Ok).
> I have set Ok button as a default button.
>
> I want that when I select a table item by double click then ok's button
> selection listener is called.
> To do this I have added to table MouseDoubleClick listener
> and do that:
> traverse(SWT.TRAVERSE_RETURN) to a dialog.
>
> But instead of ok button cancel selection listener is called.
>
> What have I done wrong?
>
> Here is code:
>
> import org.eclipse.swt.*;
> import org.eclipse.swt.events.*;
> import org.eclipse.swt.widgets.*;
> import org.eclipse.swt.custom.*;
> import org.eclipse.swt.layout.*;
>
> public class DialogProblem {
> public static void main(String[] args) {
> Display display = new Display();
> final Shell shell = new Shell(display, SWT.SHELL_TRIM);
> shell.setLayout(new FillLayout(SWT.VERTICAL));
>
> Button button = new Button(shell, SWT.PUSH);
> button.setText("Press");
>
> final Label label = new Label(shell, 0);
>
> button.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> label.setText("Pressed: " + (new DialogShell(shell)).getButton());
> }
> });
>
>
> shell.setSize(200, 200);
> shell.open();
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
>
> }
> }
>
>
> class DialogShell extends Shell implements Listener {
> Button ok, cancel;
> Composite main;
> int BORDER_OFFSET = 10;
> boolean opened = false;
> String pressedButton;
> Table table;
>
> DialogShell(Shell parent) {
> super(parent, SWT.SHELL_TRIM);
> createButtons();
> main.setLayout(new FillLayout(SWT.VERTICAL));
>
> table = new Table(main, SWT.BORDER | SWT.FULL_SELECTION);
> table.addListener(SWT.MouseDoubleClick, this);
> TableItem item = new TableItem(table, 0);
> item.setText("table item, make double click here");
> addListener(SWT.Traverse, this);
>
> layout();
> setSize(300, 200);
> }
>
> public void open() {opened = true; super.open();}
>
> public void close() {super.close(); opened = false;}
>
> public String getButton() {
> open();
> table.setFocus();
> while(opened) { if(!getDisplay().readAndDispatch())
> getDisplay().sleep(); }
> return pressedButton;
> }
>
> private void createButtons() {
>
> // set layout
> FormLayout layout = new FormLayout();
> setLayout(layout);
>
> // create buttons group
> Composite buttonsParent = new Composite(this, 0);
> FormData formData = new FormData();
> formData.left = new FormAttachment(0, BORDER_OFFSET/2);
> formData.right = new FormAttachment(100, -BORDER_OFFSET/2);
> formData.bottom = new FormAttachment(100, 0);
> buttonsParent.setLayoutData(formData);
>
> GridLayout gridLayout = new GridLayout();
> gridLayout.numColumns = 3;
> buttonsParent.setLayout(gridLayout);
>
> // create separator
> Label separator = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL|
> SWT.SHADOW_IN);
> formData = new FormData();
> formData.bottom = new FormAttachment(buttonsParent, -BORDER_OFFSET,
> SWT.TOP);
> formData.left = new FormAttachment(0, 0);
> formData.right = new FormAttachment(100, 0);
> separator.setLayoutData(formData);
>
> // create main group
> main = new Composite(this, 0);
> formData = new FormData();
> formData.top = new FormAttachment(0,0);
> formData.left = new FormAttachment(0,0);
> formData.right = new FormAttachment(100,0);
> formData.bottom = new FormAttachment(separator, 0, SWT.TOP);
> main.setLayoutData(formData);
>
> Label label = new Label(buttonsParent, 0);
> GridData d = new GridData(GridData.FILL_HORIZONTAL);
> label.setLayoutData(d);
>
> // create cancel button
> cancel = new Button(buttonsParent, SWT.PUSH);
> cancel.setText("Cancel");
> cancel.addListener(SWT.Selection, this);
> cancel.addListener(SWT.Traverse, this);
> d = new GridData(GridData.HORIZONTAL_ALIGN_END);
> d.widthHint = 80;
> cancel.setLayoutData(d);
>
> // create ok button
> ok = new Button(buttonsParent, SWT.PUSH);
> ok.setText("Ok");
> ok.addListener(SWT.Selection, this);
> ok.addListener(SWT.Traverse, this);
> d = new GridData(GridData.HORIZONTAL_ALIGN_END);
> d.widthHint = 80;
> ok.setLayoutData(d);
> setDefaultButton(ok);
> }
>
> public void handleEvent(Event event) {
> switch(event.type) {
> case SWT.Selection:
> close();
>
> if(event.widget == cancel) {
> pressedButton = "Cancel";
> return;
> }
>
> if(event.widget == ok) {
> pressedButton = "Ok";
> return;
> }
> break;
> case SWT.MouseDoubleClick:
> traverse(SWT.TRAVERSE_RETURN);
> break;
> case SWT.Traverse:
> event.doit = true;
> if(event.widget == this && event.detail == SWT.TRAVERSE_ESCAPE) {
> pressedButton = "Cancel_ESC";
> }
> break;
> }
> }
> protected void checkSubclass(){}
> }
>
Re: TRAVERSE_RETURN problem [message #438030 is a reply to message #438015] Tue, 15 June 2004 07:52 Go to previous messageGo to next message
Sergiy Uvarov is currently offline Sergiy UvarovFriend
Messages: 13
Registered: July 2009
Junior Member
Steve Northover wrote:
> Should work although your code is a bit strange. Why not just call the same
> code from 2 places instead of using traverse()?

It doesn't look strange if split this code in several classes. First
class realizes custom dialog with buttons and then, for example, you
extend this class to realize a specific dialog.

> Enter a problem report with the code. Thanks.

I found the source of problem. It was a Control traverse(int) method.
This method checks focus and if the current widget hasn't focus it sets
one. In my case it sets focus to Shell but shell sets it to the first
widget, in my code it is cancel button.
So if the main composite is created earlier then buttonsParent then
it works ok.

I am not sure that this is a bug but I don't understand why method
setFocus is called in Control.traverse(int). Can you explain this?


>
> "Sergiy Uvarov" <colonel@rts-ukraine.com> wrote in message
> news:ca9sbv$ekk$1@eclipse.org...
>
>>Hi, All.
>>
>>I have a dialog with a table and two buttons (Cancel and Ok).
>>I have set Ok button as a default button.
>>
>>I want that when I select a table item by double click then ok's button
>>selection listener is called.
>>To do this I have added to table MouseDoubleClick listener
>>and do that:
>>traverse(SWT.TRAVERSE_RETURN) to a dialog.
>>
>>But instead of ok button cancel selection listener is called.
>>
>>What have I done wrong?
>>
>>Here is code:
>>
>>import org.eclipse.swt.*;
>>import org.eclipse.swt.events.*;
>>import org.eclipse.swt.widgets.*;
>>import org.eclipse.swt.custom.*;
>>import org.eclipse.swt.layout.*;
>>
>>public class DialogProblem {
>> public static void main(String[] args) {
>> Display display = new Display();
>> final Shell shell = new Shell(display, SWT.SHELL_TRIM);
>> shell.setLayout(new FillLayout(SWT.VERTICAL));
>>
>> Button button = new Button(shell, SWT.PUSH);
>> button.setText("Press");
>>
>> final Label label = new Label(shell, 0);
>>
>> button.addSelectionListener(new SelectionAdapter() {
>>public void widgetSelected(SelectionEvent e) {
>> label.setText("Pressed: " + (new DialogShell(shell)).getButton());
>>}
>> });
>>
>>
>> shell.setSize(200, 200);
>> shell.open();
>>
>> while (!shell.isDisposed()) {
>> if (!display.readAndDispatch()) display.sleep();
>> }
>> display.dispose();
>>
>> }
>>}
>>
>>
>>class DialogShell extends Shell implements Listener {
>> Button ok, cancel;
>> Composite main;
>> int BORDER_OFFSET = 10;
>> boolean opened = false;
>> String pressedButton;
>> Table table;
>>
>> DialogShell(Shell parent) {
>> super(parent, SWT.SHELL_TRIM);
>> createButtons();
>> main.setLayout(new FillLayout(SWT.VERTICAL));
>>
>> table = new Table(main, SWT.BORDER | SWT.FULL_SELECTION);
>> table.addListener(SWT.MouseDoubleClick, this);
>> TableItem item = new TableItem(table, 0);
>> item.setText("table item, make double click here");
>> addListener(SWT.Traverse, this);
>>
>> layout();
>> setSize(300, 200);
>> }
>>
>> public void open() {opened = true; super.open();}
>>
>> public void close() {super.close(); opened = false;}
>>
>> public String getButton() {
>> open();
>> table.setFocus();
>> while(opened) { if(!getDisplay().readAndDispatch())
>>getDisplay().sleep(); }
>> return pressedButton;
>> }
>>
>> private void createButtons() {
>>
>> // set layout
>> FormLayout layout = new FormLayout();
>> setLayout(layout);
>>
>> // create buttons group
>> Composite buttonsParent = new Composite(this, 0);
>> FormData formData = new FormData();
>> formData.left = new FormAttachment(0, BORDER_OFFSET/2);
>> formData.right = new FormAttachment(100, -BORDER_OFFSET/2);
>> formData.bottom = new FormAttachment(100, 0);
>> buttonsParent.setLayoutData(formData);
>>
>> GridLayout gridLayout = new GridLayout();
>> gridLayout.numColumns = 3;
>> buttonsParent.setLayout(gridLayout);
>>
>> // create separator
>> Label separator = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL|
>>SWT.SHADOW_IN);
>> formData = new FormData();
>> formData.bottom = new FormAttachment(buttonsParent, -BORDER_OFFSET,
>>SWT.TOP);
>> formData.left = new FormAttachment(0, 0);
>> formData.right = new FormAttachment(100, 0);
>> separator.setLayoutData(formData);
>>
>> // create main group
>> main = new Composite(this, 0);
>> formData = new FormData();
>> formData.top = new FormAttachment(0,0);
>> formData.left = new FormAttachment(0,0);
>> formData.right = new FormAttachment(100,0);
>> formData.bottom = new FormAttachment(separator, 0, SWT.TOP);
>> main.setLayoutData(formData);
>>
>> Label label = new Label(buttonsParent, 0);
>> GridData d = new GridData(GridData.FILL_HORIZONTAL);
>> label.setLayoutData(d);
>>
>> // create cancel button
>> cancel = new Button(buttonsParent, SWT.PUSH);
>> cancel.setText("Cancel");
>> cancel.addListener(SWT.Selection, this);
>> cancel.addListener(SWT.Traverse, this);
>> d = new GridData(GridData.HORIZONTAL_ALIGN_END);
>> d.widthHint = 80;
>> cancel.setLayoutData(d);
>>
>> // create ok button
>> ok = new Button(buttonsParent, SWT.PUSH);
>> ok.setText("Ok");
>> ok.addListener(SWT.Selection, this);
>> ok.addListener(SWT.Traverse, this);
>> d = new GridData(GridData.HORIZONTAL_ALIGN_END);
>> d.widthHint = 80;
>> ok.setLayoutData(d);
>> setDefaultButton(ok);
>> }
>>
>> public void handleEvent(Event event) {
>> switch(event.type) {
>> case SWT.Selection:
>> close();
>>
>> if(event.widget == cancel) {
>>pressedButton = "Cancel";
>>return;
>> }
>>
>> if(event.widget == ok) {
>>pressedButton = "Ok";
>>return;
>> }
>> break;
>> case SWT.MouseDoubleClick:
>> traverse(SWT.TRAVERSE_RETURN);
>> break;
>> case SWT.Traverse:
>> event.doit = true;
>> if(event.widget == this && event.detail == SWT.TRAVERSE_ESCAPE) {
>>pressedButton = "Cancel_ESC";
>> }
>> break;
>> }
>> }
>> protected void checkSubclass(){}
>>}
>>
>
>
>
Re: TRAVERSE_RETURN problem [message #438038 is a reply to message #438030] Tue, 15 June 2004 14:55 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Check the 3.0 stream. It no longer sets focus.

"Sergiy Uvarov" <colonel@rts-ukraine.com> wrote in message
news:cam9b7$ee9$1@eclipse.org...
>
>
> Steve Northover wrote:
> > Should work although your code is a bit strange. Why not just call the
same
> > code from 2 places instead of using traverse()?
>
> It doesn't look strange if split this code in several classes. First
> class realizes custom dialog with buttons and then, for example, you
> extend this class to realize a specific dialog.
>
> > Enter a problem report with the code. Thanks.
>
> I found the source of problem. It was a Control traverse(int) method.
> This method checks focus and if the current widget hasn't focus it sets
> one. In my case it sets focus to Shell but shell sets it to the first
> widget, in my code it is cancel button.
> So if the main composite is created earlier then buttonsParent then
> it works ok.
>
> I am not sure that this is a bug but I don't understand why method
> setFocus is called in Control.traverse(int). Can you explain this?
>
>
> >
> > "Sergiy Uvarov" <colonel@rts-ukraine.com> wrote in message
> > news:ca9sbv$ekk$1@eclipse.org...
> >
> >>Hi, All.
> >>
> >>I have a dialog with a table and two buttons (Cancel and Ok).
> >>I have set Ok button as a default button.
> >>
> >>I want that when I select a table item by double click then ok's button
> >>selection listener is called.
> >>To do this I have added to table MouseDoubleClick listener
> >>and do that:
> >>traverse(SWT.TRAVERSE_RETURN) to a dialog.
> >>
> >>But instead of ok button cancel selection listener is called.
> >>
> >>What have I done wrong?
> >>
> >>Here is code:
> >>
> >>import org.eclipse.swt.*;
> >>import org.eclipse.swt.events.*;
> >>import org.eclipse.swt.widgets.*;
> >>import org.eclipse.swt.custom.*;
> >>import org.eclipse.swt.layout.*;
> >>
> >>public class DialogProblem {
> >> public static void main(String[] args) {
> >> Display display = new Display();
> >> final Shell shell = new Shell(display, SWT.SHELL_TRIM);
> >> shell.setLayout(new FillLayout(SWT.VERTICAL));
> >>
> >> Button button = new Button(shell, SWT.PUSH);
> >> button.setText("Press");
> >>
> >> final Label label = new Label(shell, 0);
> >>
> >> button.addSelectionListener(new SelectionAdapter() {
> >>public void widgetSelected(SelectionEvent e) {
> >> label.setText("Pressed: " + (new DialogShell(shell)).getButton());
> >>}
> >> });
> >>
> >>
> >> shell.setSize(200, 200);
> >> shell.open();
> >>
> >> while (!shell.isDisposed()) {
> >> if (!display.readAndDispatch()) display.sleep();
> >> }
> >> display.dispose();
> >>
> >> }
> >>}
> >>
> >>
> >>class DialogShell extends Shell implements Listener {
> >> Button ok, cancel;
> >> Composite main;
> >> int BORDER_OFFSET = 10;
> >> boolean opened = false;
> >> String pressedButton;
> >> Table table;
> >>
> >> DialogShell(Shell parent) {
> >> super(parent, SWT.SHELL_TRIM);
> >> createButtons();
> >> main.setLayout(new FillLayout(SWT.VERTICAL));
> >>
> >> table = new Table(main, SWT.BORDER | SWT.FULL_SELECTION);
> >> table.addListener(SWT.MouseDoubleClick, this);
> >> TableItem item = new TableItem(table, 0);
> >> item.setText("table item, make double click here");
> >> addListener(SWT.Traverse, this);
> >>
> >> layout();
> >> setSize(300, 200);
> >> }
> >>
> >> public void open() {opened = true; super.open();}
> >>
> >> public void close() {super.close(); opened = false;}
> >>
> >> public String getButton() {
> >> open();
> >> table.setFocus();
> >> while(opened) { if(!getDisplay().readAndDispatch())
> >>getDisplay().sleep(); }
> >> return pressedButton;
> >> }
> >>
> >> private void createButtons() {
> >>
> >> // set layout
> >> FormLayout layout = new FormLayout();
> >> setLayout(layout);
> >>
> >> // create buttons group
> >> Composite buttonsParent = new Composite(this, 0);
> >> FormData formData = new FormData();
> >> formData.left = new FormAttachment(0, BORDER_OFFSET/2);
> >> formData.right = new FormAttachment(100, -BORDER_OFFSET/2);
> >> formData.bottom = new FormAttachment(100, 0);
> >> buttonsParent.setLayoutData(formData);
> >>
> >> GridLayout gridLayout = new GridLayout();
> >> gridLayout.numColumns = 3;
> >> buttonsParent.setLayout(gridLayout);
> >>
> >> // create separator
> >> Label separator = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL|
> >>SWT.SHADOW_IN);
> >> formData = new FormData();
> >> formData.bottom = new FormAttachment(buttonsParent, -BORDER_OFFSET,
> >>SWT.TOP);
> >> formData.left = new FormAttachment(0, 0);
> >> formData.right = new FormAttachment(100, 0);
> >> separator.setLayoutData(formData);
> >>
> >> // create main group
> >> main = new Composite(this, 0);
> >> formData = new FormData();
> >> formData.top = new FormAttachment(0,0);
> >> formData.left = new FormAttachment(0,0);
> >> formData.right = new FormAttachment(100,0);
> >> formData.bottom = new FormAttachment(separator, 0, SWT.TOP);
> >> main.setLayoutData(formData);
> >>
> >> Label label = new Label(buttonsParent, 0);
> >> GridData d = new GridData(GridData.FILL_HORIZONTAL);
> >> label.setLayoutData(d);
> >>
> >> // create cancel button
> >> cancel = new Button(buttonsParent, SWT.PUSH);
> >> cancel.setText("Cancel");
> >> cancel.addListener(SWT.Selection, this);
> >> cancel.addListener(SWT.Traverse, this);
> >> d = new GridData(GridData.HORIZONTAL_ALIGN_END);
> >> d.widthHint = 80;
> >> cancel.setLayoutData(d);
> >>
> >> // create ok button
> >> ok = new Button(buttonsParent, SWT.PUSH);
> >> ok.setText("Ok");
> >> ok.addListener(SWT.Selection, this);
> >> ok.addListener(SWT.Traverse, this);
> >> d = new GridData(GridData.HORIZONTAL_ALIGN_END);
> >> d.widthHint = 80;
> >> ok.setLayoutData(d);
> >> setDefaultButton(ok);
> >> }
> >>
> >> public void handleEvent(Event event) {
> >> switch(event.type) {
> >> case SWT.Selection:
> >> close();
> >>
> >> if(event.widget == cancel) {
> >>pressedButton = "Cancel";
> >>return;
> >> }
> >>
> >> if(event.widget == ok) {
> >>pressedButton = "Ok";
> >>return;
> >> }
> >> break;
> >> case SWT.MouseDoubleClick:
> >> traverse(SWT.TRAVERSE_RETURN);
> >> break;
> >> case SWT.Traverse:
> >> event.doit = true;
> >> if(event.widget == this && event.detail == SWT.TRAVERSE_ESCAPE) {
> >>pressedButton = "Cancel_ESC";
> >> }
> >> break;
> >> }
> >> }
> >> protected void checkSubclass(){}
> >>}
> >>
> >
> >
> >
>
Re: TRAVERSE_RETURN problem [message #438106 is a reply to message #438038] Wed, 16 June 2004 06:17 Go to previous message
Sergiy Uvarov is currently offline Sergiy UvarovFriend
Messages: 13
Registered: July 2009
Junior Member
Steve Northover wrote:
> Check the 3.0 stream. It no longer sets focus.
I use 3.0M6 build under QNX and 3.0M9 build under Linux/Motif.

> "Sergiy Uvarov" <colonel@rts-ukraine.com> wrote in message
> news:cam9b7$ee9$1@eclipse.org...
>
>>
>>Steve Northover wrote:
>>
>>>Should work although your code is a bit strange. Why not just call the
>
> same
>
>>>code from 2 places instead of using traverse()?
>>
>>It doesn't look strange if split this code in several classes. First
>>class realizes custom dialog with buttons and then, for example, you
>>extend this class to realize a specific dialog.
>>
>> > Enter a problem report with the code. Thanks.
>>
>>I found the source of problem. It was a Control traverse(int) method.
>>This method checks focus and if the current widget hasn't focus it sets
>>one. In my case it sets focus to Shell but shell sets it to the first
>>widget, in my code it is cancel button.
>>So if the main composite is created earlier then buttonsParent then
>>it works ok.
>>
>>I am not sure that this is a bug but I don't understand why method
>>setFocus is called in Control.traverse(int). Can you explain this?
>>
>>
>>
>>>"Sergiy Uvarov" <colonel@rts-ukraine.com> wrote in message
>>>news:ca9sbv$ekk$1@eclipse.org...
>>>
>>>
>>>>Hi, All.
>>>>
>>>>I have a dialog with a table and two buttons (Cancel and Ok).
>>>>I have set Ok button as a default button.
>>>>
>>>>I want that when I select a table item by double click then ok's button
>>>>selection listener is called.
>>>>To do this I have added to table MouseDoubleClick listener
>>>>and do that:
>>>>traverse(SWT.TRAVERSE_RETURN) to a dialog.
>>>>
>>>>But instead of ok button cancel selection listener is called.
>>>>
>>>>What have I done wrong?
>>>>
>>>>Here is code:
>>>>
>>>>import org.eclipse.swt.*;
>>>>import org.eclipse.swt.events.*;
>>>>import org.eclipse.swt.widgets.*;
>>>>import org.eclipse.swt.custom.*;
>>>>import org.eclipse.swt.layout.*;
>>>>
>>>>public class DialogProblem {
>>>> public static void main(String[] args) {
>>>> Display display = new Display();
>>>> final Shell shell = new Shell(display, SWT.SHELL_TRIM);
>>>> shell.setLayout(new FillLayout(SWT.VERTICAL));
>>>>
>>>> Button button = new Button(shell, SWT.PUSH);
>>>> button.setText("Press");
>>>>
>>>> final Label label = new Label(shell, 0);
>>>>
>>>> button.addSelectionListener(new SelectionAdapter() {
>>>>public void widgetSelected(SelectionEvent e) {
>>>> label.setText("Pressed: " + (new DialogShell(shell)).getButton());
>>>>}
>>>> });
>>>>
>>>>
>>>> shell.setSize(200, 200);
>>>> shell.open();
>>>>
>>>> while (!shell.isDisposed()) {
>>>> if (!display.readAndDispatch()) display.sleep();
>>>> }
>>>> display.dispose();
>>>>
>>>> }
>>>>}
>>>>
>>>>
>>>>class DialogShell extends Shell implements Listener {
>>>> Button ok, cancel;
>>>> Composite main;
>>>> int BORDER_OFFSET = 10;
>>>> boolean opened = false;
>>>> String pressedButton;
>>>> Table table;
>>>>
>>>> DialogShell(Shell parent) {
>>>> super(parent, SWT.SHELL_TRIM);
>>>> createButtons();
>>>> main.setLayout(new FillLayout(SWT.VERTICAL));
>>>>
>>>> table = new Table(main, SWT.BORDER | SWT.FULL_SELECTION);
>>>> table.addListener(SWT.MouseDoubleClick, this);
>>>> TableItem item = new TableItem(table, 0);
>>>> item.setText("table item, make double click here");
>>>> addListener(SWT.Traverse, this);
>>>>
>>>> layout();
>>>> setSize(300, 200);
>>>> }
>>>>
>>>> public void open() {opened = true; super.open();}
>>>>
>>>> public void close() {super.close(); opened = false;}
>>>>
>>>> public String getButton() {
>>>> open();
>>>> table.setFocus();
>>>> while(opened) { if(!getDisplay().readAndDispatch())
>>>>getDisplay().sleep(); }
>>>> return pressedButton;
>>>> }
>>>>
>>>> private void createButtons() {
>>>>
>>>> // set layout
>>>> FormLayout layout = new FormLayout();
>>>> setLayout(layout);
>>>>
>>>> // create buttons group
>>>> Composite buttonsParent = new Composite(this, 0);
>>>> FormData formData = new FormData();
>>>> formData.left = new FormAttachment(0, BORDER_OFFSET/2);
>>>> formData.right = new FormAttachment(100, -BORDER_OFFSET/2);
>>>> formData.bottom = new FormAttachment(100, 0);
>>>> buttonsParent.setLayoutData(formData);
>>>>
>>>> GridLayout gridLayout = new GridLayout();
>>>> gridLayout.numColumns = 3;
>>>> buttonsParent.setLayout(gridLayout);
>>>>
>>>> // create separator
>>>> Label separator = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL|
>>>>SWT.SHADOW_IN);
>>>> formData = new FormData();
>>>> formData.bottom = new FormAttachment(buttonsParent, -BORDER_OFFSET,
>>>>SWT.TOP);
>>>> formData.left = new FormAttachment(0, 0);
>>>> formData.right = new FormAttachment(100, 0);
>>>> separator.setLayoutData(formData);
>>>>
>>>> // create main group
>>>> main = new Composite(this, 0);
>>>> formData = new FormData();
>>>> formData.top = new FormAttachment(0,0);
>>>> formData.left = new FormAttachment(0,0);
>>>> formData.right = new FormAttachment(100,0);
>>>> formData.bottom = new FormAttachment(separator, 0, SWT.TOP);
>>>> main.setLayoutData(formData);
>>>>
>>>> Label label = new Label(buttonsParent, 0);
>>>> GridData d = new GridData(GridData.FILL_HORIZONTAL);
>>>> label.setLayoutData(d);
>>>>
>>>> // create cancel button
>>>> cancel = new Button(buttonsParent, SWT.PUSH);
>>>> cancel.setText("Cancel");
>>>> cancel.addListener(SWT.Selection, this);
>>>> cancel.addListener(SWT.Traverse, this);
>>>> d = new GridData(GridData.HORIZONTAL_ALIGN_END);
>>>> d.widthHint = 80;
>>>> cancel.setLayoutData(d);
>>>>
>>>> // create ok button
>>>> ok = new Button(buttonsParent, SWT.PUSH);
>>>> ok.setText("Ok");
>>>> ok.addListener(SWT.Selection, this);
>>>> ok.addListener(SWT.Traverse, this);
>>>> d = new GridData(GridData.HORIZONTAL_ALIGN_END);
>>>> d.widthHint = 80;
>>>> ok.setLayoutData(d);
>>>> setDefaultButton(ok);
>>>> }
>>>>
>>>> public void handleEvent(Event event) {
>>>> switch(event.type) {
>>>> case SWT.Selection:
>>>> close();
>>>>
>>>> if(event.widget == cancel) {
>>>>pressedButton = "Cancel";
>>>>return;
>>>> }
>>>>
>>>> if(event.widget == ok) {
>>>>pressedButton = "Ok";
>>>>return;
>>>> }
>>>> break;
>>>> case SWT.MouseDoubleClick:
>>>> traverse(SWT.TRAVERSE_RETURN);
>>>> break;
>>>> case SWT.Traverse:
>>>> event.doit = true;
>>>> if(event.widget == this && event.detail == SWT.TRAVERSE_ESCAPE) {
>>>>pressedButton = "Cancel_ESC";
>>>> }
>>>> break;
>>>> }
>>>> }
>>>> protected void checkSubclass(){}
>>>>}
>>>>
>>>
>>>
>>>
>
>
Previous Topic:"Office 2003"-style menus?
Next Topic:Print preview
Goto Forum:
  


Current Time: Fri Apr 26 11:58:58 GMT 2024

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

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

Back to the top