This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
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.
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
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 ...
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.
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.