my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes problem with handling the states of the events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with handling the states of the events" Watch "problem with handling the states of the events" New topic
Author

problem with handling the states of the events

Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Hi,

Thankyou for the previous helps.

I am trying to make autmatic door system , which has some buttons on it and a JTextField at bottom , if the user enters correct the four digit then the JTextField's color turns to green else to red. and by defualt the color the JTextField is black.

I have just made the design and all my codes are in single class, now trying to complete the functuinality and then separating the classes. Now with previous helps , I have synchronized the events of four buttons at some level , but not satisfactory.

All your comments and the hlep will be much appreciated.

Code is mentioned below for more suggestions:-


Regards,
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Thamu Gurung wrote:
... I have just made the design and all my codes are in single class, now trying to complete the functuinality and then separating the classes. Now with previous helps , I have synchronized the events of four buttons at some level , but not satisfactory.

What do you mean "but not satisfactory"? Please have a look at TellTheDetails

Luck!
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Hi Pete,

I am not satisfied with the way i have dealt with the events of the buttons. As you can see in the code of the event captured " the password code to open the door is #423, but if i'm pressing 3 as the first or second or third choice , then the colour of the JTextfield changes to red", but the mechanism is that only after pressing four buttons , the colour should change .

So I want to have the change in the colour of the JTextFiled only after the user has pressed complete four buttons. How to do that ??? I could not figure out the logic ...

And how to encapsulate this event ???

Your much help will be appreciated.

Regards,
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
One way is to use a Queue<String> such as a LinkedList<String> since you want to remove the head of the queue once it holds more than four items. Then you could add the ActionEvent's actionCommand to the queue, again remove the head if you have more than four items, and then iterate through the queue to see if it matches the desired sequence.

I would also recommend that you use variable names that make sense so that your code is more self-commenting, though myself, when I've created a 2-dimensional grid of buttons like you are doing, I've had good success using a 2D array of Strings as seeds for my buttons. For e.g.,



And then create my buttons within two for loops, one nested in the other, and placing each one in a JPanel that uses an appropriate GridLayout.
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Hi Pete,

Can you please give me some hint with codes , in using the Queue<String>. I tried a lot but did not get a single idea . Its really difficult for me .


Please ..

Regards,
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
There's actually no requirement to use a queue as this can be done by simply using four String variables or an array of 4 Strings. When a key has been pressed, simply pass the 3rd variable into the 4th spot, the 2nd into the 3rd spot, the 1st into the 2nd spot, and the new String into the first spot, and then check if the 4 Strings match the password.
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Hi Pete,

Thankyou for your valuable tips.

This way it was a bit easier and smarter, thanks a lot.



Regards,

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem with handling the states of the events