Link to home
Start Free TrialLog in
Avatar of chenwei
chenwei

asked on

password dialog

I am going to generate a password dialog which has a password textfield a "ok" and a "cancel" buttons and will be called from an action in the caller calss. The password dialog must be modal. If one just click the "cancel" button, the dialog window will be disposed and do nothing. But if one click the "Ok" button, the entered password will be read first and then the dialog window will be disposed.

How can I do that?
ASKER CERTIFIED SOLUTION
Avatar of conick
conick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chenwei
chenwei

ASKER

Thanks for conik'S answer. But I don't understand what's going. First, the password should be entered from the password textfield, not from the function setPassword(s). Second, I haven't understood how can the getPAssword get the password from the password textfield. Third, if one clicks the "ok" button" on the password dialog, the password should be got, not to be set.

Could you please so kind to give me the real world example?
Avatar of chenwei

ASKER

What I want is something like JOptionPane.showInputDialog(). The difference is by showInputDialog() one can see the entered text, but what I want is entered text should not be seen.
The actual password is stored in the original frame.  That is why the password is set in the
dialog... not gotten..
I'll do a quick working example

This works for me....
I used JPasswordField and set the echo char to be '*'.
I get a depreciated warning for getText() on JPasswordText()...
let me check into what it should be...
ill append a comment in a few minutes

//in file MyDialog.java
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.border.*;

public class MyDialog extends JDialog   {
     
         
          JPasswordField password_field;
          JButton okbutton, cancelbutton;
          PasswordInterface pi;
         
     MyDialog(PasswordInterface pi)  {
         
          super((JFrame)pi,true);
          this.pi=pi;                        
         
          JPanel pane= new JPanel();
          this.getContentPane().add(pane);
          pane.setLayout(new FlowLayout(FlowLayout.CENTER));
         
          password_field= new JPasswordField(15);
          password_field.setEchoChar('*');
         
          ButtonListener buttonlistener= new ButtonListener();
         
          okbutton= new JButton("OK");
          okbutton.setActionCommand("ok");
          okbutton.addActionListener(buttonlistener);
         
          cancelbutton= new JButton("Cancel");
          cancelbutton.setActionCommand("cancel");
          cancelbutton.addActionListener(buttonlistener);
         
          pane.add(password_field);
          pane.add(okbutton);
          pane.add(cancelbutton);
     }
     
     class ButtonListener implements ActionListener  {
          public void actionPerformed(ActionEvent e)  {
               if (e.getActionCommand().equals("cancel"))  { MyDialog.this.dispose();  }
               else if (e.getActionCommand().equals("ok"))  {
                         
                    pi.setPassword(password_field.getText());

                    MyDialog.this.dispose();
               }
          }
     }
}
//in file MyFrame.java
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;

public class MyFrame extends JFrame implements PasswordInterface  {
      
     String stored_password;
     static final MyFrame frame= new MyFrame();
     JTextField field1, field2;
     JButton gobutton;
     
     public MyFrame()  {
      
         
          getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
          
          field1= new JTextField();
          field1.setColumns(10);
         
         
          getContentPane().add(field1);
         
          ButtonListener buttonlistener= new ButtonListener();
         
          gobutton = new JButton("GO");
          gobutton.addActionListener(buttonlistener);
          getContentPane().add(gobutton);

      }

     // methods for Password interface -- taken from PasswordInterface interface
     

     public String getPassword()  {
          return stored_password;
     }
     
     public void setPassword(String password_from_dialog)  {
          stored_password= password_from_dialog;
     }
     
     public static void main(String args[])  {
          //frame= new MyFrame();
          frame.addWindowListener(new WindowAdapter()  {
               public void windowClosing(WindowEvent e)  {
                    System.exit(0);
               }
          });
         
          frame.setSize(800,600);
          frame.setVisible(true);
     }    
         
     public void testPassword()  {
          field1.setText(new String(stored_password));
     }
     
     class ButtonListener implements ActionListener  {
          public void actionPerformed(ActionEvent e)  {
               
               MyDialog dialog= new MyDialog((PasswordInterface)frame);
               dialog.setSize(new Dimension(500,400));
               dialog.setLocationRelativeTo(frame);
               dialog.setVisible(true);
         
               // After Dialog is closed this code is executed
               testPassword();
          }
     }
}
//in file PasswordInterface.java
public interface PasswordInterface  {
     
     public String getPassword();
     public void setPassword(String password);

}


use getPassword() instead of getText() to get rid of the depreciation warning

The above program works on my system...
it comes up with a frame that has a textfield and a go button
click the go button
a password dialog comes up
with a JPasswordField and two buttons , ok and cancel
type in a password and hit ok and the password will appear in the original frame
instead of displaying it you probably want to check the value entered with a predetermined
password value
I displayed it so you would know what is stored
Avatar of chenwei

ASKER

You program runs by me. But there is some problem: as I click the "go" button, the password dialog is shown. But if I click the "cancel" button, you can see there is a lot message output by the dos window. It menas something runs not so perfect. Have you observated?
hmm seems to work ok on my system
if cancel is selected im just calling dispose() on the dialog....

what is the first line of the message?  
something like NullPointerException or something else?


Avatar of chenwei

ASKER

The output message is so long that I can't see the first line. But here are some lines of message:
.......
DefaultButtonModel.fireActionPerformed(Compiled Code)
DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
.......

Do you understand what these mean?
its basically going down all the steps where the error occurred..
a HUGE list of classes is pretty normal when dealing with Swing
classes...
A typical runtime error goes as follows:
line 1: gives the exception that was thrown (eg. NullPointerException, ArrayIndexOutOfBounds)
line 2: gives the first place the error is thrown.. it will say what class and what method the
exception was thrown.  
line 3+: the exception propagates up the chain becoming more general.  when an exception is thrown it be thrown in the calling class and so on and so forth... So code with a long history (a lot of calling methods) will give you a very long error list.

The first two lines are usually the ones you want to look at though (although not always).
You can get the line number that caused the problem in parenthesis.
If it says "Compiled Code" (like the line above) that means that you have the JIT compiler enabled.  You can turn it off while debugging if necessary to get the line numbers.

So basically I would need the first couple lines to determine the error.  I didnt test my sample code very well.  I just made sure it worked for what we were talking about.

--- after a minute or two ---

Ack!  I must have never actually pressed the cancel button...
whats wrong is that the variable that gets set (stored_password) is never initialized to anything
initialize it as " " or something and you wont get that message
the exception was a NullPointerException on the String

 


 
Avatar of chenwei

ASKER

o.k., thank you very much.