Custom JDialog PIN Entry

Its been a while since I did Java programming, but I occasionally get myself into a weeks programming to keep up with the hobbies. Today is the second day I got back into some programming. I am working on an application which has a settings screen, but I don’t want people to access it, so I had a task of developing a password screen. The application is being designed for a touchscreen, so passwords are out of the question (no keyboard) hence, I wanted to get a keypad on the screen for a 4-digit PIN entry.

I wanted the behaviour of the program to stop everything and only allow user input on the keypad whilst it was on screen – much like the behaviour of a JDialog¬† when it is on screen – you have to deal with it before you can get back to the parent frame. So I built a class which extends on the JDialog and constructs a JPanel with the JButtons inside to deal with the user input. Not being a Java regular, I couldn’t tell you whether I’ve done something against the rules here, but it works pretty neatly, and the best bit is its a static call, so it can be used in any other scenario (just access the variables inside for the three possible responses, CANCELLED, ACCESS_GRANTED or ACCESS_DENIED)

The keypad looks like this:

To call it, you simply add the Keypad class to your program, and use the code below

//showDialog(jFrame, title, PIN, attemptsAllowed)
int result = Keypad.showDialog(myJFrame, "Please enter PIN", "1234", 3);
//result is one of the following
switch(result){
  case Keypad.ACCESS_GRANTED:
    break;
  case Keypad.ACCESS_DENIED:
    break;
  case Keypad.CANCELLED:
    break;
}

Hope that helps someone. All that is needed to perfect this is to prevent the user closing the window using the X in the top right, but that doesn’t bother me at the moment.
I’ve put the zip file below with the class file and the images used on the buttons.

Keypad.zip