1

SO i am working on a project and i was trying to disable the frame and the field from the program so that only the window is front is this one being active here is my code :

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

public class password
{
  private static String password = "pass";
  public static void main(String[]args) {
    JFrame frame = new JFrame("Password");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,100);
    JLabel label = new JLabel("Enter password");
    JPanel panel = new JPanel();
    frame.add(panel);
    JPasswordField pass = new JPasswordField(10);
    pass.setEchoChar('*');
    pass.addActionListener(new AL());
    panel.add(label, BorderLayout.WEST);
    panel.add(pass, BorderLayout.WEST);
}
static class AL implements ActionListener
{
    public void actionPerformed(ActionEvent e) {
        JPasswordField input = (JPasswordField) e.getSource();
        char [] passy = input.getPassword();
        String p = new String(passy);
        if (p.equals(password)){
            JOptionPane.showMessageDialog(null, "Correct");
            System.out.print("Welcome to Adam's Quirky program.");



        }
        else
            JOptionPane.showMessageDialog(null, "Incorrect");
       }
    }
 }

the program i am currently using to program is Eclipse.

Jeffrey
  • 42,428
  • 7
  • 84
  • 138
Adam
  • 45
  • 1
  • 7
  • 2
    Creating a `String` from the `char[]` you get from the `JPasswordField` [defeats the purpose](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords). – Jeffrey Aug 29 '12 at 01:21
  • 1
    Just a few annotation's: 1) Naming of classes begins with upper case. 2) Don't call setSize(), call pack() method (and setVisible(true) after that) and call your frame method's in the end. (I'm telling you this because I was making same mistake's). 3) Your Border Layout has no purpose since you didn't call panel.setLayout() method at all. – Branislav Lazic Aug 29 '12 at 01:35

1 Answers1

4

I'd take a look at a modal dialog.

Either use a JOptionPane or roll your own.

If these don't suit your needs, try taking a look at JLayer (Java 7) or JXLayer (Java 6 and below)

Have a look at LockableUI (It's a little out of date, but the basic idea is the same)

If that doesn't appeal, you could use a "blocking glass pane"

Take a look at

as some examples

Community
  • 1
  • 1
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329