Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Lightbulb JPasswordTextField using JOptionPane

    I want to use JPasswordTextField in JOptionPane.showInputMessage(...) dialog instead of textfield component.
    I think that It may be possible using createDialog() Method of JOptionPane OR OptionPaneUI! but how?

    that is I want to use an Password Dialog Box like Input Dialog of JOptionPane.

    Plese tell me that how can I do this?

    Thanks in advance...

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Use JDialog directly and create your own.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    1. There's a Swing section where this should have been posted.
    2. There's no class called JPasswordTextField in the standard API.
    3. Any component can be passed as the "message" argument to showMessageDialog.

    db

  4. #4
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Talking [SOVED] By Alternative Way

    Darryl.Burke:
    There's a Swing section where this should have been posted.
    Yes, I too agree with you. I must fillow this rule in future.

    Darryl.Burke:
    There's no class called JPasswordTextField in the standard API.
    SORRY! there is a class JPasswordField in JAVA API that I wanted to mention.

    masijade:
    Use JDialog directlys and create your own.
    I had that idea in my mind but I put this question b'coz I just wanted to know the concepts of "JOptionPane component settings" and "how to use OptionPaneUI"

    I created my own showPasswordDialog(...) function which is attached. Rename the MyPasswordFieldText.txt to MyPasswordFieldText.java and run it.

    Darryl.Burke:
    Any component can be passed as the "message" argument to showMessageDialog
    Thanks, but my purpose is not soved using this method.
    e.g.
    Java Code:
    String password=JOptionPane.showInputDialog(null,new JPasswordField(20));
    System.out.println(password);
    then I will not get password which will be typed in password field....kindly update my further knowledge regarding passing component as message.
    What is the use of it?
    Attached Files Attached Files

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Java Code:
    import javax.swing.*;
    
    public class MyPass2 {
       public MyPass2() {
          
       }
    
       private static void createAndShowUI() {
          JPasswordField pField = new JPasswordField(10);
          JPanel pPanel = new JPanel();
          pPanel.add(new JLabel("Please Enter Password: "));
          pPanel.add(pField);
          
          int result = JOptionPane.showConfirmDialog(null, pPanel);
          if (result == JOptionPane.OK_OPTION) {
             System.out.println(String.valueOf(pField.getPassword()));
          }
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  6. #6
    Join Date
    Aug 2007
    Posts
    45
    Rep Power
    0

    Post Thanks

    Thanks for told me the uses of passing controls to JOptionPane.show*(...) functions.

    I modified your code as below:-
    Java Code:
    import javax.swing.*;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    
    public class MyPass2 {
       public MyPass2() {
          
       }
    
       private static void createAndShowUI() {
       	JPasswordField pField = new JPasswordField(20);
          GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            c.insets.top = 4;
            c.insets.bottom = 4;
            JPanel pPanel = new JPanel(gridbag);
            pPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20));
            c.anchor = GridBagConstraints.WEST;
          pPanel.add(new JLabel("Please Enter Password: "),c);
          c.gridy=1;
          pPanel.add(pField,c);
          int result = JOptionPane.showConfirmDialog(null, pPanel,"Password",JOptionPane.OK_CANCEL_OPTION);
          if (result == JOptionPane.OK_OPTION) {
             System.out.println(String.valueOf(pField.getPassword()));
          }
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Only problem is that default focus is OK button istead of JPasswordField;
    If we add following lines then we will get focus properly:-
    Java Code:
    JOptionPane jp=new JOptionPane(pPanel,JOptionPane.OK_CANCEL_OPTION);
    pPanel.requestFocusInWindow();
    jp.createDialog().setVisible(true);
    but we need to write action listener for ok button to find out wheather OK button is pressed or not?

    please modify my above code such that I will get default focus to password field.
    Last edited by Gajesh Tripathi; 10-19-2009 at 11:19 AM.

Similar Threads

  1. help with JOptionPane.showMessageDialog()
    By gmn1 in forum New To Java
    Replies: 8
    Last Post: 03-30-2009, 09:31 AM
  2. JOptionpane
    By tommyyyy in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 08:33 AM
  3. JOptionPane.showInputDialog
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-25-2009, 06:04 AM
  4. JOptionPane
    By Mir in forum New To Java
    Replies: 5
    Last Post: 11-29-2008, 02:04 AM
  5. JOptionPane
    By whosadork in forum New To Java
    Replies: 2
    Last Post: 10-23-2008, 02:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •