Password field color - swing

I want to change the color of my JPasswordField with key Listener. I'm making a registration form and the user should fill the passwordfield at least with 8 characters that include digits and letters. Can anybody help me?
my code :
enter code here
public void keyPressed(KeyEvent e) {
if(e.getSource()==passwordField){
if(passwordField.toString().length()>=8)
passwordField.setBackground(Color.GREEN);
else
passwordField.setBackground(Color.RED);
}
}

When the keyPressed() event is fired the Document of the password field has not yet been updated, so the length will be 1 less than you think it should be.
Instead try using the keyTyped() method:
public void keyTyped(KeyEvent e)
{
JPasswordField password = (JPasswordField)e.getSource();
if(passwordField.getPassword().length >= 8)
passwordField.setBackground(Color.GREEN);
else
passwordField.setBackground(Color.RED);
}
Also, when writing a listener you should get the source of the event from the event object instead of trying to access an instance variable.
You may also want to consider using an InputVerifier on this field. The input verifier will prevent the user from tab away from this field unless at least 8 digits have been entered.
Note: even using the keyTyped() event you can still have problems because if the user uses the "BackSpace" key no event is generated. So maybe you should be using the keyRelased() event. Even this can cause a problem because if the users holds down a key multiple characters will be entered into the field before a keyReleased event is fired.
The best solution is to use a Document Listener. Read the section from the Swing tutorial on How to Write a Document Listener for more information.

you're doing it wrong
change to this
public void keyPressed(KeyEvent e) {
if(e.getSource()==passwordField){
if(passwordField.getPassword().length()>=8)
passwordField.setBackground(Color.GREEN);
else
passwordField.setBackground(Color.RED);
}
}
you should use getPassword()

Related

AS3 Have whatever I type into Text Input turn into a variable?

I'm fairly new to AS3 and I want to know how would I go about having whatever is typed into a Text Input to become a variable? For example, in a text input under name if I were to type in "Jeffrey" that would become a variable and I could then use that to display "Hello, Jeffrey" or something like that with whatever the name inputted is replaced with Jeffrey.
It's fairly simple to implement this. One way you could do this is have a keyboard event running while input is being made. Then assuming you have the textfield created and ready for input, you have a variable that stores the textfield text property. So you would create your variable:
private var inputText:String;
Next add your listener:
stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
Then create the listener function, and in that function set your variable to the textfield text. In this example let's assume your textfield is called myTextField:
private function onKeyDown( e:KeyboardEvent ):void {
textInput = myTextField.text;
}
Now you have a variable that is storing the textfield's text every time a key is pressed.
Whenever you are done with the input, remove the listener from the stage:
stage.removeEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
Now there is also an alternative way. You can attach a TEXT_INPUT event to the textfield and capture the input that way, like so:
myTextField.addEventListener( TextEvent.TEXT_INPUT, onInput );
public function onInput( e:TextEvent ):void {
inputString = myTextField.text;
}
There are two parts to this:
1: Define "something that is typed into a TextInput".
The reason for this is that each thing is typed in one keystroke at a time. This is important, because for the word "Jeffrey", you have several different things that get typed into there:
J
Je
Jef
Jeff
Jeffr
Jeffre
Jeffrey
You probably don't want all of those turned into variables, but ActionScript 3.0 will more-or-less think of those as all separate inputs. Somehow you have to distinguish "Jeffrey" from all the rest. Usually this is done by making the user click a Submit button or tab off the TextInput or something, but you'll have to decide.
2: Add a dictionary variable to your class and use the input as one of its keys:
private var myDict:Dictionary = new Dictionary();
Whenever the program decides that the user has finished typing something into the TextInput (like in a Submit button's click handler), just do this:
myDict[myTextInput.text] = new Object(); // or whatever value you want
So let's say the user entered the value "Jeffrey". Then you could come back later and access what is effectively a variable by that name:
myDict[myTextInput.text] = 5; // myTextInput.text == "Jeffrey"
.
.
.
trace(myDict["Jeffrey"]); // output is "5"
trace(myDict.Jeffrey); // output is "5"
trace(myDict[myTextInput.text]); // output is "5" if myTextInput.text is the same
Effectively rolling back an earlier edit, just go ahead and use a Dictionary, not an Object. If the user types in certain things like "12345", things will just go better if you use a Dictionary.

Make Flex TextInput show no prompt on empty String

I am using a s:TextInput in Flex 4.5. It shows it's prompt text if the underlying text value is null or empty String. Does anybody know if I can make either don't show the prompt on empty String or even show a different prompt?
I already found a way by extending the TextInput class and overriding some of the methods but I am still hoping anyone here knows an easier way ;-)
Ok, so based on the comments, here it is:
You store the current prompt value in a private variable, like so:
private var _inputPrompt:String = "";
Then you create a getter, so the value is accessible from outside of this class:
public function get inputPrompt():String
{
return _inputPrompt;
}
Now you can bind inputPrompt anywhere you need it, however, the problem is the getter won't be recalled once the private value changes. You can fix this very easily: Create an update method, for example like so:
public function updateInputPrompt(value:String):void
{
_inputPrompt = value;
}
Ok, nothing fancy so far. I'm guessing this is the point where you are right now. In order to "force" the getter to be recalled, you have to bind it to an event, like so:
[Bindable(event="inputPromptUpdated")]
public function get inputPrompt():String
{
return _inputPrompt;
}
Finally, you can simply dispatch this event when the value is update (i.e. in the updateInputPrompt method):
public function updateInputPrompt(value:String):void
{
_inputPrompt = value;
dispatchEvent("inputPromptUpdated"); // For binding
}
This way, the getter will be recalled every time you dispatch that event.
Hope this helps. Have a great day, and a great weekend!

How to get on keyPressed in a JPanel CTRL+m?

I'm trying to implement a JPopupMenu over a text editor component. It should be activated on CTRL+m. Can I do that inside
#Override
public void keyPressed(KeyEvent arg0) {
}
and if yes, how? Because if I try
if(arg0.isControlDown()&&arg0.getKeyChar()=='m')
it doesn't work.
At first I thought it was something to do with CTRL+M being the same thing as a carriage-return/line feed, but that wasn't true. Try:
if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_M) {
System.out.println("pressed");
menu.setVisible(true);
}
I couldn't get it to work using chars with e.getKeyChar() either, but the getKeyCode() works for me. Great, it works. But I'm the type that has to know why. So I found this:
KEY_PRESSED and KEY_RELEASED events
are not intended for reporting of
character input. Therefore, the values
returned by this method are guaranteed
to be meaningful only for KEY_TYPED
events

Programmatically create table, enter values in it, retrieve the entire contents

When I go to parse through the table, all the stuff I created programmatically is missing...
I must be forgetting something so that it's losing all the stuff that I built programmatically (I am only getting the shell of the table).
Any ideas?
If I put the table in a Session object after I programmatically create it then it works except all the values the user enters will not be there obviously.
protected void btnSave_Click(object sender, EventArgs e)
{
SaveMainGrid(tblCC);
// SaveMainGrid((System.Web.UI.WebControls.Table)Session["tblMain"]);
}
private void SaveMainGrid(Control control)
{
foreach (Control ctrl in control.Controls)
{
if (ctrl is RadNumericTextBox)
{
RadNumericTextBox t = ctrl as RadNumericTextBox;
}
else
{
if (ctrl.Controls.Count > 0)
{
SaveMainGrid(ctrl);
}
}
}
}
This is likely another case of asp.net lifecycle. If you dynamically create controls, they aren't going to be available in the viewstate unless you recreate them. Even then, I don't know that the values would be persisted.
(I think they might. It's been a while since I fought this particular Web Forms quark.)
Check this question out for more info.
Why are you creating the table dynamically? Can you use an asp:GridView?

ComboBox Bug in ActionScript

I was trying to filter a combo box dataprovider based on the values in the text boxes . When the contents of the dataprovider changes Combo box automatically calls change event method . Please find the sample code below.
Filter Utility Function:
private function filterLocations(event:FocusEvent):void {
locationsList1.filterFunction = filterUtility;
locationsList1.refresh();
}
public function filterUtility(item:Object):Boolean {
// pass back whether the location square foot is with in the range specified
if((item.SQUARE_FOOTAGE >= rangeText1.text) && (item.SQUARE_FOOTAGE rangeText2.text))
return item.SQUARE_FOOTAGE;
}
// THIS WOULD BE CALLED WHEN COMBO BOX SELECTION IS DONE
private function selectLocationsReports(event:ListEvent):void {
selectedItem =(event.currentTarget as ComboBox).selectedItem.LOCATION_ID;
}
When the DataProvider gets refreshed its automatically calls change method and was throwing Null Pointer function because its prematurely calling the above selectLocationsReports method and its throwing error.
Can somebody let me know how to stop the CHANGE event from propogation when the dataprovider is refreshed.
You can't stop a CHANGE event, just don't add an event listener unless you are prepared to get the event. I don't see where your event listener for Event.CHANGE is in the code above.
Just be sure that you don't addEventListener(Event.CHANGE, selectLocationsReports) until your ComboBox is ready for it.
The other thing to do (on top of Kekoa's response) is put an if statement in the event handler, and check to make sure the data is there before you begin working with it.
A handy syntax I use frequently for this is
if(dataprovidername) {
}