Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Password Field Help.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Password Field Help.

    Following an tutorial I manage to create a screen that will ask for a Username and a password, but my purpose is not to create a login screen is to capture the info that the user put over there.

    Here is my code:

    //By José D. Hernández, 2013.
     
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.KeyEvent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
     
    public class passwordpr 
    {
    	public static void main(String args[]) 
    	{
    		String title = "La Union";
    		JFrame frame = new JFrame(title);
    		Container content = frame.getContentPane();
     
    		JPanel userPanel = new JPanel(new BorderLayout());
    		JLabel userLabel = new JLabel("Username: ");
    		userLabel.setDisplayedMnemonic(KeyEvent.VK_U);
    		JTextField userTextField = new JTextField();
    		userLabel.setLabelFor(userTextField);
    		userPanel.add(userLabel, BorderLayout.WEST);
    		userPanel.add(userTextField, BorderLayout.CENTER);
     
    		JPanel passPanel = new JPanel(new BorderLayout());
    		JLabel passLabel = new JLabel("Password: ");
    		passLabel.setDisplayedMnemonic(KeyEvent.VK_P);
    		JPasswordField passTextField = new JPasswordField();
    		passLabel.setLabelFor(passTextField);
    		passPanel.add(passLabel, BorderLayout.WEST);
    		passPanel.add(passTextField, BorderLayout.CENTER);
     
    		JPanel panel = new JPanel(new BorderLayout());
    		panel.add(userPanel, BorderLayout.NORTH);
    		panel.add(passPanel, BorderLayout.SOUTH);
    		content.add(panel, BorderLayout.NORTH);
     
    		frame.setSize(250, 150);
    		frame.setVisible(true);
    	}
    }

    How can I capture the info that the user input in "Username" and "password"? Thanks. and much more specials to Norm.

    I hope you guys are not tired of me.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Password Field Help.

    The classes where the user enters the data have methods you can call to get the data.
    One way to know when the user has finished entering the data is to have a button that he can press.
    Add a listener to the button which will be called when the user presses the button.
    The listener method can then call the methods for the classes where the user has entered the data to get what was entered.

    See the tutorial: How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    JosPhantasmE (January 23rd, 2013)

Similar Threads

  1. How to add date field ?
    By Rajiv in forum Web Frameworks
    Replies: 1
    Last Post: August 10th, 2011, 09:27 PM
  2. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  3. Using a password field
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 11:54 AM
  4. password field, with focused "Enter" button
    By chronoz13 in forum Java Swing Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM
  5. password field, with focused "Enter" button
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM