6
+ − 1 import java.awt.*;
+ − 2 import java.awt.event.*;
+ − 3 import javax.swing.*;
+ − 4 import java.util.concurrent.CountDownLatch;
+ − 5
+ − 6 public class PasswordAsker {
+ − 7
+ − 8 static String password;
+ − 9 static JFrame frame;
+ − 10 static CountDownLatch latch;
+ − 11
+ − 12
+ − 13 public PasswordAsker() {
+ − 14 password = null;
+ − 15 javax.swing.SwingUtilities.invokeLater(new Runnable() {
+ − 16 public void run() {
+ − 17 createAndShowGUI();
+ − 18 }
+ − 19 });
+ − 20 latch = new CountDownLatch(1);
+ − 21 }
+ − 22
+ − 23
+ − 24 private static void createAndShowGUI() {
+ − 25 //Create and set up the window.
+ − 26 frame = new JFrame("Password");
+ − 27 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ − 28 frame.setContentPane(setMainPane());
+ − 29
+ − 30 //Display the window.
+ − 31 frame.pack();
+ − 32 frame.setVisible(true);
+ − 33 }
+ − 34
+ − 35
+ − 36 private static JPanel setMainPane() {
+ − 37 JPanel rootPanel = new JPanel(false);
+ − 38 rootPanel.setLayout(new GridLayout(0, 1));
+ − 39
+ − 40 JPanel infoPanel = new JPanel(false);
+ − 41 JLabel infoLabel = new JLabel("Please write here the password that you entered for the mySQL root account.\r\nNo information is stored nor sent. I promise.");
+ − 42 infoPanel.add(infoLabel);
+ − 43
+ − 44 JPanel passPanel = new JPanel(false);
+ − 45 passPanel.setLayout(new GridLayout(1, 0));
+ − 46 JLabel passLabel = new JLabel("password");
+ − 47 final JTextField passText = new JTextField(20);
+ − 48 passLabel.setLabelFor(passText);
+ − 49 passPanel.add(passLabel);
+ − 50 passPanel.add(passText);
+ − 51
+ − 52 JPanel okPanel = new JPanel(false);
+ − 53 JButton okButton = new JButton("OK");
+ − 54 okPanel.add(okButton);
+ − 55
+ − 56 okButton.addActionListener(new ActionListener() {
+ − 57 public void actionPerformed(ActionEvent e) {
+ − 58 password = passText.getText();
+ − 59 frame.setVisible(false);
+ − 60 frame.dispose();
+ − 61 latch.countDown();
+ − 62 }
+ − 63 });
+ − 64
+ − 65 rootPanel.add(infoPanel);
+ − 66 rootPanel.add(passPanel);
+ − 67 rootPanel.add(okPanel);
+ − 68
+ − 69 return rootPanel;
+ − 70 }
+ − 71
+ − 72
+ − 73 public boolean waitForPassword() {
+ − 74 try {
+ − 75 latch.await();
+ − 76 }
+ − 77 catch (InterruptedException e) {
+ − 78 return false;
+ − 79 }
+ − 80 return true;
+ − 81 }
+ − 82
+ − 83
+ − 84 public String getPassword() {
+ − 85 return password;
+ − 86 }
+ − 87 }