0% found this document useful (0 votes)
3K views9 pages

Ayush Practical No 12

This document contains 3 Java programs demonstrating the use of JTextField and JPasswordField: 1. A program that sets the password character of a JPasswordField to '#' instead of '*' when typing a password. 2. A program that uses a JPasswordField and JTextField to demonstrate user authentication by checking a username and password on login. 3. A program that uses a JPasswordField to accept a password from the user and displays an error message if the password length is less than 6 characters.

Uploaded by

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

Ayush Practical No 12

This document contains 3 Java programs demonstrating the use of JTextField and JPasswordField: 1. A program that sets the password character of a JPasswordField to '#' instead of '*' when typing a password. 2. A program that uses a JPasswordField and JTextField to demonstrate user authentication by checking a username and password on login. 3. A program that uses a JPasswordField to accept a password from the user and displays an error message if the password length is less than 6 characters.

Uploaded by

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

ADVANCE JAVA PROGRAMMING

Name :- Ayush Maoti Wadje Class : - CO5I


Roll No :-76

Practical No 12 :- Write a Program To Demonstrate The Use Of JTextField


And JPasswordField Using Listener Interfae.

X. Program Code.

1. Write a Program Using JPasswordField To Set The Password Character as „#‟


Instead of “*”.

Code :-

import javax.swing.*;
public class PasswordFieldExample
{
public static void main(String[] args)
{
JFrame F=new JFrame("Password Field Example");
JPasswordField value = new JPasswordField();
value.setEchoChar('#');
JLabel l1=new JLabel(" Password:");
l1.setBounds(20,100, 80,30);
value.setBounds(110,110,110,40);
F.add(value);
F.add(l1);
F.setSize(330,350);
F.setLayout(null);
F.setVisible(true);
}
}

1
Output :-

XIII. Exercise.
1 Write a Program Using JPasswordField And JTextField To Demonstrate The Use
Of User Authetication.

Code :-

import javax.swing.*;
public class PasswordFieldExample1 {
public static void main(String[] args)
{
JFrame F=new JFrame("Password Field Example");
final JLabel label = new JLabel();
label.setBounds(20,150, 200,50);
final JPasswordField value = new JPasswordField();
value.setBounds(100,75,100,30);
JLabel L1=new JLabel("Username:");
L1.setBounds(20,20, 80,30);
JLabel L2=new JLabel("Password:");
L2.setBounds(20,75, 80,30);
JButton B = new JButton("Login");
B.setBounds(100,120, 80,30);

2
final JTextField text = new JTextField();
text.setBounds(100,20, 100,30);
F.add(value);
F.add(L1);
F.add(label);
F.add(L2);
F.add(B);
F.add(text);
F.setSize(300,350);
F.setLayout(null);
F.setVisible(true);

}
}

Output :-

3
2. Write a Program Using JTextField To Perform The Addition Of Two Numbers.

Code :-

import javax.swing.*;

import java.awt.event.*;

public class TextFieldExample implements ActionListener{

JTextField tf1,tf2,tf3;

JButton b1,b2;

TextFieldExample(){

JFrame f= new JFrame();

tf1=new JTextField();

tf1.setBounds(50,50,150,20);

tf2=new JTextField();

tf2.setBounds(50,100,150,20);

tf3=new JTextField();

tf3.setBounds(50,150,150,20);

4
tf3.setEditable(false); b1=new

JButton("+");

b1.setBounds(50,200,50,50);

b1.addActionListener(this);

f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

public void actionPerformed(ActionEvent e) {

String s1=tf1.getText();

String s2=tf2.getText();

int a=Integer.parseInt(s1);

int b=Integer.parseInt(s2);

int c=0;

if(e.getSource()==b1){

c=a+b;

String result=String.valueOf(c);

tf3.setText(result);

public static void main(String[] args) {

new TextFieldExample();

5
}

Output :-

6
3. Write a Program Using JPasswordField To Accept Password From User And If
The Length Must Be Less Than 6 Character Then Error Message Should Be
Displayed “Password Length Must Be >6 Character”

Code :-

import javax.swing.*;
import java.awt.event.*;
public class PasswordFieldeEx1 {
public static void main(String[] args)
{
JFrame F=new JFrame("Password Field Example");

final JPasswordField value = new JPasswordField();


value.setBounds(100,75,100,30);
JLabel L1=new JLabel("Username:-");
L1.setBounds(20,20, 80,30);
JLabel L2=new JLabel("Password:-");
L2.setBounds(20,75, 80,30);
JButton B = new JButton("Login👍");
B.setBounds(100,120, 80,30);
final JTextField text = new JTextField();
text.setBounds(100,20, 100,30);
F.add(value);
F.add(L1);
F.add(L2);
F.add(B);
F.add(text);
F.setSize(300,300);
F.setLayout(null);
F.setVisible(true);
B.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event) {
event.getSource();
char[] password = value.getPassword();
if (password.length < 6) {
7
System.out.println("Password length must be >6 characters");
} else {
System.out.println("Login successfully");
}
}

});
}
}

Output:- :-

8
9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy