Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 64
Registered: ‎05-09-2009
My Device: Not Specified

PasswordEditField set password char

Hello all,

 

I'm creating a PasswordEditField with the ability to set the password character. Something similair to the .net PasswordBox.PasswordChar property: http://msdn.microsoft.com/en-us/library/system.windows.controls.passwordbox.passwordchar.aspx

 

When i set the password char to '\0' the field shows the password as plain text. 

 

The code i've written works ok except the cursor is not positioned correct when the password is shown in plain thext and i'm editing the field.

This is what i have so far; 

 

 

import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.component.PasswordEditField; public class ExtendedPasswordEditField extends PasswordEditField { private char passwordChar = '*'; public ExtendedPasswordEditField() { super(); } public ExtendedPasswordEditField(String label, String initialValue, int maxNumChars, long style) { super(label, initialValue, maxNumChars, style); } public ExtendedPasswordEditField(String label, String initialValue) { super(label, initialValue); } public char getPasswordChar() { return passwordChar; } public void setPasswordChar(char passwordChar) { this.passwordChar = passwordChar; invalidate(); } public void paint(Graphics graphics) { if (passwordChar == '\0') { graphics.drawText(getText(), 0, 0); } else { StringBuffer passwordMask = new StringBuffer(); for (int i = 0; i < getText().length(); i++) { passwordMask.append(passwordChar); } graphics.drawText(passwordMask.toString(), 0, 0); } } }

 

 
Any ideas what i'm missing?

 

Thanks!

 

Developer
Posts: 64
Registered: ‎05-09-2009
My Device: Not Specified

Re: PasswordEditField set password char

It seems i have to paint the cursor myself. Does anyone have an example how to do this?
BlackBerry Development Advisor
Posts: 15,697
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: PasswordEditField set password char

[ Edited ]
There is another way you could accomplish this.  Override the keyChar method to capture the user's input, append the character to a local StringBuffer defined within your custom field and then call setText on the field, adding an additional "password character".  This won't require you to override the paint method.  You'd also need to either override or create a custom method to return the actual password value.
Message Edited by MSohm on 06-15-2009 04:36 PM
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker