Java JPasswordField

Java JPasswordField

Java JPasswordField
JPasswordField class is a text component that allows single line text just like JTextField.It's used to enter password.

How to Declare JPasswordField class

This is how you declare java.swing.JPasswordField class.
[public class JPasswordField extends JTextField]

 JPasswordField used constructors

  • JPasswordField()-creates a JPasswordField with null text.
  • JPasswordField(String s)-creates a JPasswordField with a text.
  • JPasswordField(int columns)-creates an empty JPasswordField with specified number of columns 
  • JPasswordField(int columns, Strings)-creates a JPasswordField with specified number of columns and a text.

JPasswordField Example

[
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
public class JPasswordFieldExample extends JFrame {

    JPasswordFieldExample()
    {
        super("JPasswordFieldExample");
        setLayout(null);
        setLocationRelativeTo(null);
        JLabel label=new JLabel("Password");
        JPasswordField pass=new JPasswordField();
        label.setBounds(20,100, 80,30);
        pass.setBounds(100,100,100,30);
        add(label);
        add(pass);
        setSize(400,400);
        setVisible(true);
    }
    public static void main(String args[])
    {
        new JPasswordFieldExample();
    }
}
]

Ouput

java.swing.JPasswordField



JPasswordField with ActionListener  Example 

[
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class JPasswordFieldExample extends JFrame implements ActionListener{
    public JPasswordField pass;
    public JTextField user;
    JPasswordFieldExample()
    {
        super("JPasswordFieldExample");
        try        {
            UIManager.setLookAndFeel
                    ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        setLayout(null);
        setLocationRelativeTo(null);


        pass = new JPasswordField();

        pass.setBounds(100,75,100,30);
        JLabel label1=new JLabel("Username:");
        label1.setBounds(20,20, 80,30);
        JLabel label2=new JLabel("Password:");
        label2.setBounds(20,75, 80,30);
        JButton button = new JButton("Login");
        button.setToolTipText("Click here to login");
        button.addActionListener(this);
        button.setBounds(100,120, 80,30);
        user = new JTextField();
        user.setBounds(100,20, 100,30);

        add(label1);
        add(label2);
        add(user);
        add(pass);
        add(button);
        setSize(400,400);
        setVisible(true);
    }
    public static void main(String args[])
    {
        new JPasswordFieldExample();
    }
    public void actionPerformed(ActionEvent event)
    {
        String username=user.getText();
        String password= (String)pass.getText();
        JOptionPane.showMessageDialog(this, "Username Entered is="+username+
                        "\n Password is="+password,"Simple Login",
                JOptionPane.INFORMATION_MESSAGE);

    }
}
]

Output


Lessons to learn from Above Program

I have introduced several methods and swing components that i have talked about in my previous posts.These are.


Main Lesson

The main lesson to learn today is how to create a simple login form with username and JPasswordField.
In my next lessons i will connect the login form with a database that verifies login credentials entered by the user.


COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JPasswordField
Java JPasswordField
Java JPasswordField
https://3.bp.blogspot.com/-jAvrYd2mknE/WL3QypcUWcI/AAAAAAAAHDw/t_PXT2M71DMJ5ttFkn2xZqHvN6ltoo8YQCLcB/s200/JPasswordField-example-min.PNG
https://3.bp.blogspot.com/-jAvrYd2mknE/WL3QypcUWcI/AAAAAAAAHDw/t_PXT2M71DMJ5ttFkn2xZqHvN6ltoo8YQCLcB/s72-c/JPasswordField-example-min.PNG
Java Codes
http://www.download-all.net/2017/03/java-jpasswordfield.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jpasswordfield.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy