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

R.practical 12th

Advance java practical no 12

Uploaded by

gourupatil88300
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)
21 views9 pages

R.practical 12th

Advance java practical no 12

Uploaded by

gourupatil88300
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

Subject - AJP [22517]

Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________

X - PROGRAM CODE
Q ------------------------
------------------------------------------- PROGRAM CODE -----------------------------------------------------------
import javax.swing.*;
import java.awt.*;
public class P12XCode extends JPasswordField
{
public P12XCode(int columns)
{
super(columns);
}
@Override
public char getEchoChar()
{
return '#';
}
public static void main(String[] args)
{
JFrame f = new JFrame("To Set The Password Character As '#' instead of
'*' ");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());
P12XCode pf = new P12XCode(20);
pf.setEchoChar('#');
JButton PB = new JButton("Show Password");
PB.addActionListener(e ->
{
char echoChar = pf.getEchoChar();
if (echoChar == 0)
{
pf.setEchoChar('#');
}
else
{
pf.setEchoChar((char) 0);
}
});
JButton B1 = new JButton("Hide Password");
B1.addActionListener(e ->
{
char echoChar = pf.getEchoChar();
if (echoChar == -1)
{
pf.setEchoChar((char) 0);
}
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________

else
{
pf.setEchoChar('#');
}
});
f.add(pf);
f.add(PB);
f.add(B1);
f.pack();
f.setVisible(true);
f.setSize(500, 200);
}
}

-------------------------------------------- PROGRAM OUTPUT --------------------------------------------------


Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________

XIII. Exercise
Q1 ------------------------
------------------------------------------- PROGRAM CODE -----------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class P12ExQ1


{
private static final String VALID_USERNAME = "Atharva Bodhankar";
private static final String VALID_PASSWORD = "mustang1969";
public static void main(String[] args)
{
JFrame f = new JFrame("User Authentication Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JPanel logP = new JPanel(new GridLayout(3, 2, 20, 20));
JLabel UL = new JLabel("User-name:");
JLabel pL = new JLabel("Password:");
JTextField userfi = new JTextField();
JPasswordField pf = new JPasswordField();
JButton logb = new JButton("Log in");
logb.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String username = userfi.getText();
char[] pchar = pf.getPassword();
String password = new String(pchar);
if (isValidUser(username, password))
{
JOptionPane.showMessageDialog(f, "Login Successful!");
}
else
{
JOptionPane.showMessageDialog(f, "Invalid Username or
Password", "Error", JOptionPane.ERROR_MESSAGE);
}
pf.setText("");
}
});
logP.add(UL);
logP.add(userfi);
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________

logP.add(pL);
logP.add(pf);
logP.add(logb);
f.add(logP, BorderLayout.CENTER);
f.setSize(300, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static boolean isValidUser(String username, String password)
{
return username.equals(VALID_USERNAME) &&
password.equals(VALID_PASSWORD);
}
}

-------------------------------------------- PROGRAM OUTPUT --------------------------------------------------


Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________

Q2 ------------------------
------------------------------------------- PROGRAM CODE -----------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class P12ExQ2


{
public static void main(String[] args)
{
JFrame f = new JFrame("To Perform The Addition Of Two Numbers");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JPanel P1 = new JPanel(new GridLayout(2, 2, 10, 10));
JTextField f1 = new JTextField();
JTextField f2 = new JTextField();
JButton b1 = new JButton("Add The Numbers ");
JTextField f3 = new JTextField();
b1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
double num1 = Double.parseDouble(f1.getText());
double num2 = Double.parseDouble(f2.getText());
double num3 = num1 + num2;
f3.setText(Double.toString(num3));
}
catch (NumberFormatException ex)
{
f3.setText("Invalid input");
}
}
});
P1.add(new JLabel("1st Number : "));
P1.add(f1);
P1.add(new JLabel("2nd Number : "));
P1.add(f2);
f.add(P1, BorderLayout.NORTH);
f.add(b1, BorderLayout.EAST);
f.add(f3, BorderLayout.SOUTH);
f3.setEditable(false);
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________

f.setSize(400, 150);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
-------------------------------------------- PROGRAM OUTPUT --------------------------------------------------
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________

Q3 ------------------------
------------------------------------------- PROGRAM CODE -----------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class P12ExQ3


{
public static void main(String[] args)
{
JFrame f = new JFrame("To Accept Password From User With Six
Charecters");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JPanel pan = new JPanel(new FlowLayout());
JPasswordField pf = new JPasswordField(20);
JButton sb = new JButton("Submit");
sb.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
char[] pc = pf.getPassword();
String password = new String(pc);
if (password.length() < 6)
{
JOptionPane.showMessageDialog(f, "Ivalid Password - Please
Enter Six or More Charecters", "Error",
JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(f, " Valid Password !");
}
pf.setText("");
}
});
pan.add(new JLabel("Enter Password:"));
pan.add(pf);
pan.add(sb);
f.add(pan, BorderLayout.CENTER);
f.setSize(300, 150);
f.setLocationRelativeTo(null);
f.setVisible(true);
Subject - AJP [22517]
Practical No. 12: Write A Program To Demonstrate The Use Of Jtextfield And Jpasswordfield Using
Listener Interface..
Performed By – Vedant Nawade
CO5I – 44
_________________________________________________________________________________________________________________________________

f.setSize(300, 200);
}
}

-------------------------------------------- PROGRAM OUTPUT --------------------------------------------------

You might also like