Results 1 to 8 of 8
  1. #1
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default How to get the text from PasswordField?

    Hi,

    i need to develop a java program in which it will ask the user "Username" and "Password" and by which it will login to a wesite.

    I have developed a GUI in which the username should be entered in the User "JTextfield" and Password in Password "JPasswordField". I need to make sure that password should not be displayed in the GUI.

    but I didnt find a way to parse the string (char) entered in the "JPasswordField" to the website.

    It is always passing a 'null' character! I tried to search in internet and i found that it wont be able to pass the passwordfield characters but you will be able to check in the main function itself by comparing with another char array in the main program itself whether the password entered is correct or not


    Is there any alternate way for this?

    Objective : Parse the password (which is not displayed in the GUI) to a website or some other function where it will use it

    thanks,
    Aswin kavali
    Last edited by aswinkavali; 11-12-2014 at 03:10 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: How to get the text from PasswordField?

    What have you tried? Where is your MCVE?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: How to get the text from PasswordField?

    Hi Kevin,

    I tried to pass the password and the username to a perl file where it will recieve it as arguments (test.pl Username password) and the perl will write those into a text file. I can see the username is printed and the Password is not getting printed.

    Below is the sample program which i used....
    .
    .
    .
    passw = new String(passwordField.getPassword());


    Process process;
    String p = "perl " + "test.pl " + passw + " " + User_text.getText() ;
    try
    {
    process = Runtime.getRuntime().exec(p); ..........

    Thanks,
    Ashwin

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: How to get the text from PasswordField?

    This code contains two separate passwords: passwordField and User_text. Can you please provide an MCVE?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: How to get the text from PasswordField?

    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.text.ParseException;
    import javax.swing.*;
    import javax.swing.text.MaskFormatter;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.io.*;

    public class Front_end {

    private JFrame frmBugsar;
    private JTextField User_text;
    private JPasswordField passwordField;
    public String Username,Passw;
    private JTextField textField;
    public String passw;
    private JFormattedTextField textField_1;
    private JFormattedTextField textField_2;
    private MaskFormatter fe;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    Front_end window = new Front_end();
    window.frmBugsar.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the application.
    */
    public Front_end() {
    initialize();
    }


    /**
    * Initialize the contents of the frame.
    */

    private void initialize() {
    frmBugsar = new JFrame();
    Image img = new ImageIcon(this.getClass().getResource("/icon.png")).getImage();
    //lblNewLabel.setIcon(new ImageIcon(img));
    frmBugsar.setIconImage(img);
    frmBugsar.setTitle("BUGSAR");
    frmBugsar.setBounds(100, 100, 376, 243);
    frmBugsar.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
    frmBugsar.getContentPane().setLayout(null);

    JLabel USERNAME = new JLabel("User name");
    USERNAME.setBounds(174, 63, 61, 14);
    frmBugsar.getContentPane().add(USERNAME);

    JLabel Password = new JLabel("Password");
    Password.setBounds(174, 88, 61, 14);
    frmBugsar.getContentPane().add(Password);

    User_text = new JTextField(System.getProperty("user.name"));
    User_text.setBounds(245, 60, 86, 20);
    frmBugsar.getContentPane().add(User_text);
    User_text.setColumns(10);

    passwordField = new JPasswordField();
    passwordField.setBounds(245, 85, 85, 20);
    frmBugsar.getContentPane().add(passwordField);
    passw = new String(passwordField.getPassword());


    JButton REPORT = new JButton("GENERATE REPORT");
    REPORT.addActionListener(new ActionListener() {


    public void actionPerformed(ActionEvent event) {

    Process process;
    String p = "perl " + "test.pl " + passw + " " + User_text.getText() ;
    try
    {
    process = Runtime.getRuntime().exec(p);
    process.waitFor();
    if(process.exitValue() == 0)
    {
    JOptionPane.showMessageDialog(null, "Export Success");
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Export failed");
    }
    }
    catch(Exception e)
    {
    JOptionPane.showMessageDialog(null, "Exception: "+ e.toString());

    }


    }
    });
    REPORT.setBounds(174, 157, 152, 23);
    frmBugsar.getContentPane().add(REPORT);

    JLabel lblNewLabel = new JLabel("");
    //Image img = new ImageIcon(this.getClass().getResource("/icon.png")).getImage();
    lblNewLabel.setIcon(new ImageIcon(img));
    lblNewLabel.setBounds(10, 40, 109, 112);
    frmBugsar.getContentPane().add(lblNewLabel);


    }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to get the text from PasswordField?

    You are reading from the password field before you've even displayed the screen.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: How to get the text from PasswordField?

    You set your passw variable as soon as you create the JPasswordField, which means the passw variable will be the empty String.

    Perhaps you want to set the passw variable after the user actually types something?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  8. #8
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: How to get the text from PasswordField?

    Oops!

    Thanks Kevin.

    It is working now!

Similar Threads

  1. Replies: 0
    Last Post: 07-24-2012, 08:07 AM
  2. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  3. Replies: 4
    Last Post: 05-30-2011, 02:53 AM
  4. Replies: 2
    Last Post: 05-30-2011, 02:38 AM
  5. Replies: 0
    Last Post: 12-09-2010, 05:56 PM

Posting Permissions

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