0

How do I get a value returned as opposed to println which I can not read, in the following script? I am using Groovy inside Filemaker so need an explicit return 'answer' i.e. the password or a TRUE, and I do not seem to understand how to make the code wait till it finally gets to the point where the correct password is entered.

import groovy.swing.SwingBuilder
import java.awt.event.ActionListener
import java.awt.event.ActionEvent
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE

count = 2
def answer

SwingBuilder.build{
    frame( id:'root', title:'Enter your PASSWORD', location:[100,100], show:true, pack:true, defaultCloseOperation:DISPOSE_ON_CLOSE){
        //lookAndFeel 'nimbus'
        flowLayout()
        label('Password')
        passwordField(id:'pass', columns:12)
        button('click', actionPerformed:{
        ActionEvent e->
            if (pass.text == 'password'){
            optionPane().showMessageDialog( root, 'OK')

            //RETURN something at this point only when I get here

            dispose()
            } else if (count == 1) {
            optionPane().showMessageDialog(root, 'INCORRECT PASSWORD\nPROCEDURE HALTED', 'PASSWORD', 2)
            dispose()
            } else {
            count--
            message = count ==1 ?'try':'tries'
            optionPane().showMessageDialog(root, "${count} ${message} left", 'NOT VALID', 0)
            }// end if
            pass.text=''
        }// end action
        )// end button
    }
}
john renfrew
  • 393
  • 1
  • 9
  • 30

1 Answers1

2

Add a boolean field called successfulLogin and set it to true when the pass.text.equals("password")

When do you actually make your frame visible? You might want to just go with JOptionPane.

import javax.swing.JComponent
import javax.swing.JPasswordField
import javax.swing.JLabel
import javax.swing.JOptionPane

public boolean loginSuccessful() throws Exception {
    final JPasswordField passwordField = new JPasswordField();
    final JComponent[] components = [new JLabel("Password"), passwordField];
    if (JOptionPane.showConfirmDialog(null, components, "Enter your PASSWORD", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
        // user clicked ok
        if (new String(passwordField.getPassword()).equals("password")) {
            // password matches
            return true;
        }
    }
    return false;
}
Sam Barnum
  • 10,559
  • 3
  • 54
  • 60