Results 1 to 9 of 9
  1. #1
    gfunk03 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Buttons being clicked in certain order in GUI

    Hi, I'm quite new to Java and am doing an assignment where I have to create a GUI to represent a keypad system to enter a room. There are 9 buttons and the user must click 4 buttons in a certain order to gain entry eg button1, then button2, then button3, then button4.

    I can't figure out a way of coding it so it will check for the right buttons being clicked in the right order - can anyone help?

    Thanks

    Gillian

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    It's an interesting assignment and has nothing to do with the Java programming language. The problem would be the same with many different languages. You're looking for an algorithm. It reminds me of a Finite State Machine problem. From the starting state, one button takes you to the second state. If in the second state, the wrong button is pressed you go back to the starting state. If the correct button is pressed you go on to the third state. And so on.
    You keep track of what state you are in and associated with that state is the one button that will move you on to the next state. If you get a wrong button, go back to the starting state. This might be done with if/else if or with a switch statement.

    What kind of class is it that you are taking?

  3. #3
    gfunk03 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi, thanks for the reply. I will read up on algorithms. I was trying to do it with if/else statements and I could it working where it would check that the correct buttons were being pressed but not checking the order.
    The course is a night class at college, OCN leve 3, so it's not very advanced

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I would just set an "action command" for every button (ie. "1", "2", "3"...). Every time a button is pressed you append the action command of the button to a String. After all 4 buttons are pressed you can compare the String of action commands with a string that represents the the order of the four buttons (ie. "1357").

  5. #5
    gfunk03 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi, thanks for the reply. The only thing I can't figure out is how to enter the 4 action commands into a string without each one overriding the previous one?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    I can't figure out is how to enter the 4 action commands into a string
    Are you concatenating them?

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    You can also use a StringBuilder and then just invoke the toString() method whenever you want to do your compare.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Heck, I would do it even simpler than that, just use four variables and with each button press, place the value held by the the 3rd variable into the 4th, the value held by the 2nd into the 3rd, the value held by the 1st into the second, and the new value from the button press into the first. Then check the values held by each variable in order (this could be an array if you wish) and see if it matches your desired pattern. This is kind of a poor-man's first-in first-out pipe, I guess.

Similar Threads

  1. form appear after ok button clicked
    By sks in forum NetBeans
    Replies: 1
    Last Post: 06-01-2011, 08:50 AM
  2. show panel when button clicked
    By Aggror in forum New To Java
    Replies: 6
    Last Post: 10-14-2010, 04:05 PM
  3. witch buttom was clicked?
    By p0rnstar in forum New To Java
    Replies: 6
    Last Post: 01-28-2010, 04:59 PM
  4. MouseListener - clearing a textField when clicked on?!
    By sailor_girl in forum AWT / Swing
    Replies: 4
    Last Post: 03-01-2009, 05:01 PM
  5. Replies: 11
    Last Post: 02-13-2009, 12:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •