Solved

password dialog

Posted on 1998-11-06
11
348 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
  • 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 71
Unhandled exception type Exception 18 31
login form jsp example 2 25
ejb on wildfly 5 20
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

772 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