Jump to content
Server Maintenance This Week. ×

A password dialogue input function


This topic is 4447 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Has a bit of help from Sam Barnum on StackOverflow with this but have a function which either gives you three chances to enter the correct password, or returns the password you enter into the box.

Just a bit of practice for me with SwingBuilder but might be a useful starting point for others.

Some of this has been enabled by the latest Groovy 1.8.4 version in the current Scriptmaster


// PasswordSwing ( title ; message ; type ; password ; returnPass )

// 12_02_21 JR

// v1.0

// type -1 plain, 0 error, 1 information, 2 warning, 3 question

// if returnPass then displays a dialogbox with a password field and returns the passsword on OK

// and uses title and password parameters and returns entered text

// else gives three attempts to enter a password which matches password parameter

// returns true if match if not or dialog cancelled returns false





import javax.swing.JComponent

import groovy.swing.SwingBuilder



type = type.toInteger()

i = 0

left = ''

swing = new SwingBuilder()



if(returnPass){

swing.edt{

  passwordField = passwordField()

  JComponent[] components = [label(message), passwordField]

  result = optionPane().showConfirmDialog(null, components, title, 2, type)

}

return passwordField.getPassword().toString()

} else {

swing.edt{

  passwordField = passwordField()

  while(i < 3){

   JComponent[] components = [label('Password'+left), passwordField]

   result = optionPane().showConfirmDialog(null, components, title, 2, -1)

   if (passwordField.getPassword().toString() == password || result == -1 || result == 2){

    i=3

   } else if (passwordField.getPassword().toString() != password && i==2) {

    result = optionPane().showMessageDialog(null, '<html><h1><font color="red">WRONG PASSWORD</font></h1><h3>Script halted</html>', 'ERROR', 2)

    i=3

   } else {

    result = -1

    i++

    message = i == 2 ? ' try':' tries'

    left = ' - ' + (3-i) + "${message}" + ' left'

    passwordField = passwordField()

   } //end if

  } //end while

} //end swing

return result == 0 ? true:false

}



Link to comment
Share on other sites

This topic is 4447 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.