0% found this document useful (0 votes)
19 views9 pages

JAVA_PRACTICAL12

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

JAVA_PRACTICAL12

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name:- Priyanka Khandu Thadke

Class:-A(CO5I) Roll no:- 60


Practical no:-12 Write a pogram to demonstrate the use of JTextField and JPasswordField
using listener interface.
Program code:-
Q. Write a program using JPasswordField to set he password character as ‘#’ instead of ‘*’.

import java.awt.*;

import javax.swing.*;

public class JPasswordFieldDemo {

public static void main(String args[]) {

JFrame f = new JFrame("JPasswordField and JTextField");

f.setLayout(null);

f.setSize(300, 300);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel l1 = new JLabel("Email ID:");

JLabel l2 = new JLabel("Password:");

JTextField jtf = new JTextField(15);

JPasswordField jpf = new JPasswordField(15);

jpf.setEchoChar('#');

JButton jb = new JButton("Submit");

l1.setBounds(20, 50, 80, 30);

l2.setBounds(20, 100, 80, 30);

jtf.setBounds(100, 50, 150, 30);

jpf.setBounds(100, 100, 150, 30);

jb.setBounds(100, 150, 100, 30);


f.add(l1);

f.add(l2);

f.add(jtf);

f.add(jpf);

f.add(jb);

f.getContentPane().setBackground(Color.YELLOW);

f.setVisible(true);

}
}
Output:-

Exercise:-
Q.1) Write a program using JPasswordField and JTextField to demonstrate the use of user
authentication.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;
public class Authentication extends JFrame implements ActionListener {

JButton button;

JTextField jtf;

JPasswordField jpf;

JLabel jl, j2;

public Authentication() {

setVisible(true);

setTitle("Authentication Frame");

setSize(300, 300);

setLayout(new FlowLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jl = new JLabel("User Name:");

j2 = new JLabel("Password:");

jtf = new JTextField(15);

jpf = new JPasswordField(15);

button = new JButton("Submit");

add(jl);

add(jtf);

add(j2);

add(jpf);

add(button);

button.addActionListener(this);

}
public void actionPerformed(ActionEvent e) {

String s1, s2;

s1 = jtf.getText();

s2 = new String(jpf.getPassword());

if (s1.equals("ABC") && s2.equals("123")) {

JOptionPane.showMessageDialog(this, "Authenticated User");

} else {

JOptionPane.showMessageDialog(this, "Unauthorized User");

public static void main(String[] args) {

new Authentication(); }
}
Output:-

Q.2)Write a program using JTextField to perform the addition of two numbers.


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class AdditionOfTwoNum extends JFrame implements ActionListener {

JButton button;

JTextField jtf1, jtf2, jtf3;

JLabel j1, j2;

public AdditionOfTwoNum() {

setVisible(true);

setTitle("Addition Frame");

setSize(300, 300);

setLayout(new FlowLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

j1 = new JLabel("First Number:");

j2 = new JLabel("Second Number:");

jtf1 = new JTextField(15);

jtf2 = new JTextField(15);

jtf3 = new JTextField(15);

jtf3.setEditable(false);

button = new JButton("Addition");

add(j1);

add(jtf1);

add(j2);
add(jtf2);

add(button);

add(jtf3);

button.addActionListener(this);

public void actionPerformed(ActionEvent e) {

try {

int n1 = Integer.parseInt(jtf1.getText());

int n2 = Integer.parseInt(jtf2.getText());

int n3 = n1 + n2;

jtf3.setText(Integer.toString(n3));

} catch (NumberFormatException ex) {

JOptionPane.showMessageDialog(this, "Please enter valid numbers");

public static void main(String[] args) {

new AdditionOfTwoNum();

}
}

Output:-
Q.3) Write a java program using JPasswordField l to accept password from the user if the
length is less than 6 characters, then error message should be displayed." Password length
must be greater than 6 characters”.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class PasswordCheck extends JFrame {

private JPasswordField passwordField;

private JLabel messageLabel;

public PasswordCheck() {

setTitle("Password Checker");

setSize(300, 150);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

passwordField = new JPasswordField(20);

messageLabel = new JLabel("Enter your password:");

JButton checkButton = new JButton("Submit");

checkButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


checkPassword();

});

setLayout(new FlowLayout());

add(messageLabel);

add(passwordField);

add(checkButton);

private void checkPassword() {

String password = new String(passwordField.getPassword());

if (password.length() < 6) {

JOptionPane.showMessageDialog(this, "Password length must be greater than 6


characters.", "Error", JOptionPane.ERROR_MESSAGE);

} else {

JOptionPane.showMessageDialog(this, "Password accepted!", "Success",


JOptionPane.INFORMATION_MESSAGE);

} }

public static void main(String[] args) {

PasswordCheck frame = new PasswordCheck();

frame.setVisible(true);

}}

Output:-

You might also like