0

I need some help, I have found this password program on the Internet, it is perfect, but there is one problem.What I would like to do, once the password is verified, launch my other program that I have made, the other program that I have made is HTA (Hyper Text Application), is there anyway that I can do this? Here is the code:

package components;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

/* PasswordDemo.java requires no other files. */

public class PasswordDemo extends JPanel
                      implements ActionListener {
private static String OK = "ok";
private static String HELP = "help";

private JFrame controllingFrame; //needed for dialogs
private JPasswordField passwordField;

public PasswordDemo(JFrame f) {
    //Use the default FlowLayout.
    controllingFrame = f;

    //Create everything.
    passwordField = new JPasswordField(10);
    passwordField.setActionCommand(OK);
    passwordField.addActionListener(this);

    JLabel label = new JLabel("Enter the password: ");
    label.setLabelFor(passwordField);

    JComponent buttonPane = createButtonPanel();

    //Lay out everything.
    JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    textPane.add(label);
    textPane.add(passwordField);

    add(textPane);
    add(buttonPane);
}

protected JComponent createButtonPanel() {
    JPanel p = new JPanel(new GridLayout(0,1));
    JButton okButton = new JButton("OK");
    JButton helpButton = new JButton("Help");

    okButton.setActionCommand(OK);
    helpButton.setActionCommand(HELP);
    okButton.addActionListener(this);
    helpButton.addActionListener(this);

    p.add(okButton);
    p.add(helpButton);

    return p;
}

public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();

    if (OK.equals(cmd)) { //Process the password.
        char[] input = passwordField.getPassword();
        if (isPasswordCorrect(input)) {
            JOptionPane.showMessageDialog(controllingFrame,
                "Success! You typed the right password.");
        } else {
            JOptionPane.showMessageDialog(controllingFrame,
                "Invalid password. Try again.",
                "Error Message",
                JOptionPane.ERROR_MESSAGE);
        }

        //Zero out the possible password, for security.
        Arrays.fill(input, '0');

        passwordField.selectAll();
        resetFocus();
    } else { //The user has asked for help.
        JOptionPane.showMessageDialog(controllingFrame,
            "You can get the password by searching this example's\n"
          + "source code for the string \"correctPassword\".\n"
          + "Or look at the section How to Use Password Fields in\n"
          + "the components section of The Java Tutorial.");
    }
}

/**
 * Checks the passed-in array against the correct password.
 * After this method returns, you should invoke eraseArray
 * on the passed-in array.
 */
private static boolean isPasswordCorrect(char[] input) {
    boolean isCorrect = true;
    char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };

    if (input.length != correctPassword.length) {
        isCorrect = false;
    } else {
        isCorrect = Arrays.equals (input, correctPassword);
    }

    //Zero out the password.
    Arrays.fill(correctPassword,'0');

    return isCorrect;
}

//Must be called from the event dispatch thread.
protected void resetFocus() {
    passwordField.requestFocusInWindow();
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("PasswordDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    final PasswordDemo newContentPane = new PasswordDemo(frame);
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Make sure the focus goes to the right component
    //whenever the frame is initially given the focus.
    frame.addWindowListener(new WindowAdapter() {
        public void windowActivated(WindowEvent e) {
            newContentPane.resetFocus();
        }
    });

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            //Turn off metal's use of bold fonts
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    createAndShowGUI();
        }
    });
}
}
Petzl11
  • 161
  • 1
  • 3
  • 18
  • 2
    What restricts the user from just opening let's say `HTA.exe`? – skiwi Dec 16 '13 at 09:19
  • Try with runtime.exec().. it might work... – TheLostMind Dec 16 '13 at 09:20
  • @skiwi That's exactly what I was going to ask next... – Petzl11 Dec 16 '13 at 09:23
  • May be this will help you [ProcessBuilder](http://stackoverflow.com/questions/5604698/java-programming-call-an-exe-from-java-and-passing-parameters) – eatSleepCode Dec 16 '13 at 09:26
  • 1
    Run the example from [this answer](http://stackoverflow.com/a/20286447/2587435). It will give you your desired result. – Paul Samsotha Dec 16 '13 at 09:31
  • @peeskillet This is perfect, but how would I make it after the password is accepted launch my program that is an external one? – Petzl11 Dec 16 '13 at 09:39
  • What type of program is it? – Paul Samsotha Dec 16 '13 at 09:44
  • @peeskillet I used HTA, it is a hta program, all I rely need is for the program you gave me to have a file path so if the password and username are correct it will run the program. – Petzl11 Dec 16 '13 at 09:54
  • Can anyone else help me to add a `file path` to this code http://stackoverflow.com/questions/7323086/java-swing-how-can-i-implement-a-login-screen-before-showing-a-jframe/20286447#20286447, After the password has been verified and to launch an external program. – Petzl11 Dec 16 '13 at 10:22

1 Answers1

2

Instead of using JFrame for password application you can use JDialog.
If password is correct

if (isPasswordCorrect(input)) {
         //Give call to class which creates JFrame for your application.
        }
Sachin
  • 3,424
  • 3
  • 21
  • 42
  • Thanks, but what I need to do is launch an external application.I did give you a 1 up. – Petzl11 Dec 16 '13 at 09:24
  • @Petzl11 If you want to make that application independent of this code use Runtime runtime=Runtime.getRuntime().exec("command to execute HTA app") – Sachin Dec 16 '13 at 09:28
  • Thanks, but where would I put this code and what is `command to execute app` is that the File path? – Petzl11 Dec 16 '13 at 09:30
  • 1
    @Petzl11: It will be the way you invoke you standalone application. In case Java, generally we use java -cp path of jars classname which has main method – Sachin Dec 16 '13 at 09:33