Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT.PASSWORD and enter key on Mac(Want to have a listener on my Text box that has the SWT.PASSWORD)
icon5.gif  SWT.PASSWORD and enter key on Mac [message #508058] Fri, 15 January 2010 17:27 Go to next message
dwain Missing name is currently offline dwain Missing nameFriend
Messages: 35
Registered: October 2009
Member
I am building an RCP application that must run on multiple platforms. I have a login screen that shows up when you first load the application with a textbox for your username and one for your password. I have a keylistener on both text boxes that listens for the enter key. If you hit the enter key while in the username box, it just goes to the password box, if you hit the enter key in the password box, it calls the login function. This works perfectly in Windows and Linux. However on a Mac it does not work. It seems that you can't have a key listener on an Text widget with the SWT.PASSWORD style in Mac.

I also tried a selection listener, since the enter key often calls the defaultWidgetSelected(Event e) method of the selection listener. That also works perfectly in Windows but not at all in Mac. I really would like a way to hit the enter key after you type in your password and have that call my login method, any ideas?

Thank you.
Re: SWT.PASSWORD and enter key on Mac [message #508388 is a reply to message #508058] Mon, 18 January 2010 15:51 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi,

You're seeing a bug in swt 3.5.x that is fixed in the 3.6 stream, so if
you're willing/able to move up to a 3.6 stable release then this should work
for you. Another option would be to use swt's 3.5.x carbon port since it
does not have this problem (though we're generally encouraging apps to adopt
the cocoa port as much as possible).

If neither of these options appeal then you could work around this problem
by changing your dialog's implementation to something like the snippet
below, which is functionally equivalent, but just a bit awkward.

public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,200,200);
shell.setLayout(new GridLayout(2, false));
new Label(shell, SWT.NONE).setText("Name:");
final Text nameText = new Text(shell, SWT.SINGLE);
nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
nameText.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
e.doit = false; // don't invoke the default button
nameText.traverse(SWT.TRAVERSE_TAB_NEXT);
}
}
});
new Label(shell, SWT.NONE).setText("Password:");
new Text(shell, SWT.PASSWORD).setLayoutData(new
GridData(GridData.FILL_HORIZONTAL));
Button login = new Button(shell, SWT.PUSH);
login.setText("Login");
shell.setDefaultButton(login);
login.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("login!");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}

Grant


"dwain" <dwain.bethel@vanns.net> wrote in message
news:hiq8hc$rpf$1@build.eclipse.org...
> I am building an RCP application that must run on multiple platforms. I
have a login screen that shows up when you first load the application with a
textbox for your username and one for your password. I have a keylistener
on both text boxes that listens for the enter key. If you hit the enter key
while in the username box, it just goes to the password box, if you hit the
enter key in the password box, it calls the login function. This works
perfectly in Windows and Linux. However on a Mac it does not work. It
seems that you can't have a key listener on an Text widget with the
SWT.PASSWORD style in Mac.
>
> I also tried a selection listener, since the enter key often calls the
defaultWidgetSelected(Event e) method of the selection listener. That also
works perfectly in Windows but not at all in Mac. I really would like a way
to hit the enter key after you type in your password and have that call my
login method, any ideas?
>
> Thank you.
Re: SWT.PASSWORD and enter key on Mac [message #508389 is a reply to message #508388] Mon, 18 January 2010 15:55 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
In the last reply, "3.6 stable release" should have been "3.6 milestone
release". The first 3.6.x stable release will be in June 2010.

Grant


"Grant Gayed" <grant_gayed@ca.ibm.com> wrote in message
news:hj2018$sqp$1@build.eclipse.org...
> Hi,
>
> You're seeing a bug in swt 3.5.x that is fixed in the 3.6 stream, so if
> you're willing/able to move up to a 3.6 stable release then this should
work
> for you. Another option would be to use swt's 3.5.x carbon port since it
> does not have this problem (though we're generally encouraging apps to
adopt
> the cocoa port as much as possible).
>
> If neither of these options appeal then you could work around this problem
> by changing your dialog's implementation to something like the snippet
> below, which is functionally equivalent, but just a bit awkward.
>
> public static void main(String[] args) {
> final Display display = new Display();
> Shell shell = new Shell(display);
> shell.setBounds(10,10,200,200);
> shell.setLayout(new GridLayout(2, false));
> new Label(shell, SWT.NONE).setText("Name:");
> final Text nameText = new Text(shell, SWT.SINGLE);
> nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
> nameText.addTraverseListener(new TraverseListener() {
> public void keyTraversed(TraverseEvent e) {
> if (e.detail == SWT.TRAVERSE_RETURN) {
> e.doit = false; // don't invoke the default button
> nameText.traverse(SWT.TRAVERSE_TAB_NEXT);
> }
> }
> });
> new Label(shell, SWT.NONE).setText("Password:");
> new Text(shell, SWT.PASSWORD).setLayoutData(new
> GridData(GridData.FILL_HORIZONTAL));
> Button login = new Button(shell, SWT.PUSH);
> login.setText("Login");
> shell.setDefaultButton(login);
> login.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent e) {
> System.out.println("login!");
> }
> });
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
> }
>
> Grant
>
>
> "dwain" <dwain.bethel@vanns.net> wrote in message
> news:hiq8hc$rpf$1@build.eclipse.org...
> > I am building an RCP application that must run on multiple platforms. I
> have a login screen that shows up when you first load the application with
a
> textbox for your username and one for your password. I have a keylistener
> on both text boxes that listens for the enter key. If you hit the enter
key
> while in the username box, it just goes to the password box, if you hit
the
> enter key in the password box, it calls the login function. This works
> perfectly in Windows and Linux. However on a Mac it does not work. It
> seems that you can't have a key listener on an Text widget with the
> SWT.PASSWORD style in Mac.
> >
> > I also tried a selection listener, since the enter key often calls the
> defaultWidgetSelected(Event e) method of the selection listener. That
also
> works perfectly in Windows but not at all in Mac. I really would like a
way
> to hit the enter key after you type in your password and have that call my
> login method, any ideas?
> >
> > Thank you.
>
>
Re: SWT.PASSWORD and enter key on Mac [message #513579 is a reply to message #508058] Wed, 10 February 2010 15:49 Go to previous messageGo to next message
Gabriel Erzse is currently offline Gabriel ErzseFriend
Messages: 6
Registered: November 2009
Junior Member
Another way to work around this, if you cannot upgrade to the latest version of SWT, is something like this:

int style = SWT.BORDER;
if (!Utils.isAppleMacOS()) style |= SWT.PASSWORD;
Text passwordText = new Text(composite, style);
if (Utils.isAppleMacOS()) passwordText.setEchoChar('\u2022');


where
Utils.isAppleMacOS()
is a utility method that tells you when you are running on Mac (so that the behavior on other OSes is not altered).

Basically you just make the Text field a regular one, and set the echo char to the bullet sign so that it looks like a password field. This way your listeners get notified about events on the field.
Re: SWT.PASSWORD and enter key on Mac [message #513630 is a reply to message #513579] Wed, 10 February 2010 17:49 Go to previous message
dwain Missing name is currently offline dwain Missing nameFriend
Messages: 35
Registered: October 2009
Member
Thanks, that is a good idea as well. I actually extended Text as my own Text widget. Then in the constructor I remove the SWT.PASSWORD bit when I call the super constructor. Then if they sent in the SWT.PASSWORD bit, i set the echo char.

public MyText(Composite parent, int style)
	{
	super(parent, ( style & SWT.PASSWORD) != 0 ? style ^ SWT.PASSWORD : style);
		
	if((style & SWT.PASSWORD) != 0)
		this.setEchoChar('\u2022');
        }
Previous Topic:Adding a button to show preferences dialog in toolbar
Next Topic:Strange window height problem
Goto Forum:
  


Current Time: Mon Dec 09 11:23:12 GMT 2024

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

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

Back to the top