Solved

password dialog

Posted on 1998-11-06
11
365 Views
Last Modified: 2012-05-04
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?
0
Comment
Question by:chenwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 7

Accepted Solution

by:
conick earned 50 total points
ID: 1227023
You can use an interface as a "go between" for dialogs and their parent frames.

public interface PasswordPref {
       public String getPassword ();
       public void setPassword(String s);
    }

    public class MainFrame extends Frame implements PasswordPrefl {
          String password;
           public String getPassword () {
                 return password;
           }
      }
       public void setPassword(String s){
            password= s;
       }

    }

    public class PasswordDialog extends Dialog {
        public PasswordDialog(PasswordPref pp)
       {
            //put your TextField and buttons here
       }

       public void actionPerformed (...)
      {
           //when OK is clicked call pp.setPassword()
          // then call dispose on your dialog
      }


Let me know if you need a real world example

0
 

Author Comment

by:chenwei
ID: 1227024
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?
0
 

Author Comment

by:chenwei
ID: 1227025
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.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 7

Expert Comment

by:conick
ID: 1227026
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

0
 
LVL 7

Expert Comment

by:conick
ID: 1227027
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);

}


0
 
LVL 7

Expert Comment

by:conick
ID: 1227028
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
0
 

Author Comment

by:chenwei
ID: 1227029
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?
0
 
LVL 7

Expert Comment

by:conick
ID: 1227030
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?


0
 

Author Comment

by:chenwei
ID: 1227031
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?
0
 
LVL 7

Expert Comment

by:conick
ID: 1227032
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

 


 
0
 

Author Comment

by:chenwei
ID: 1227033
o.k., thank you very much.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question