PRACTICAL NO.
10
Write a program to generate KeyEvent when a key is pressed and display "Key Pressed" message.
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class KeyPressEventGenerator extends JFrame implements KeyListener {
private JLabel label;
public KeyPressEventGenerator() {
label = new JLabel("Press a key...");
add(label);
addKeyListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
setVisible(true);
public void keyPressed(KeyEvent e) {
label.setText("Key Pressed: " + KeyEvent.getKeyText(e.getKeyCode()));
public void keyReleased(KeyEvent e) {
// Not needed for this example
public void keyTyped(KeyEvent e) {
// Not needed for this example
public static void main(String[] args) {
new KeyPressEventGenerator();
OUTPUT:-
Develop a program which will implement special keys such as function keys and arrow keys.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class SpecialKeysDemo extends JFrame implements ActionListener {
public SpecialKeysDemo() {
setTitle("Special Keys Demo");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
handleKeyPressed(evt);
});
setVisible(true);
private void handleKeyPressed(KeyEvent evt) {
int keyCode = evt.getKeyCode();
if (keyCode >= KeyEvent.VK_F1 && keyCode <= KeyEvent.VK_F12) {
System.out.println("Function key F" + (keyCode - KeyEvent.VK_F1 + 1) + " pressed");
} else if (keyCode == KeyEvent.VK_UP) {
System.out.println("Up arrow key pressed");
} else if (keyCode == KeyEvent.VK_DOWN) {
System.out.println("Down arrow key pressed");
} else if (keyCode == KeyEvent.VK_LEFT) {
System.out.println("Left arrow key pressed");
} else if (keyCode == KeyEvent.VK_RIGHT) {
System.out.println("Right arrow key pressed");
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new SpecialKeysDemo();
});
@Override
public void actionPerformed(ActionEvent e) {
// Handle other action events if needed
OUTPUT:-
PRACTICAL NO.11
Write a program to count the number of clicks performed by the user in a Frame window
import java.awt.*;
import java.awt.event.*;
public class ClickCounter extends Frame {
private int clickCount;
public ClickCounter() {
clickCount = 0
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickCount++;
System.out.println("Click Count: " + clickCount);
}};
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
setSize(300, 200);
setVisible(true);
public static void main(String[] args) {
new ClickCounter();
OUTPUT
Write a program to demonstrate the use of mouseDragged and mouseMoved method of
MouseMotionListener
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseMotionDemo extends JFrame implements MouseMotionListener {
private JLabel statusLabel;
public MouseMotionDemo() {
setTitle("Mouse Motion Demo");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
statusLabel = new JLabel("Mouse status: ");
add(statusLabel, BorderLayout.SOUTH);
addMouseMotionListener(this);
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
statusLabel.setText("Mouse dragged at (" + x + ", " + y + ")");
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
statusLabel.setText("Mouse moved at (" + x + ", " + y + ")");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MouseMotionDemo demo = new MouseMotionDemo();
demo.setVisible(true);
});
OUTPUT
PRACTICAL NO.12
Write a program using JPasswordField to set the password character as '#' instead of '*'
import javax.swing.*;
import java.awt.event.*;
public class PasswordFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Password Field Example");
JPasswordField passwordField = new JPasswordField();
// Set the password character to '#'
passwordField.setEchoChar('#');
JButton showPasswordButton = new JButton("Show Password");
showPasswordButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
char[] password = passwordField.getPassword();
JOptionPane.showMessageDialog(frame, "Password: " + new String(password));
});
frame.add(passwordField);
frame.add(showPasswordButton);
frame.setLayout(new java.awt.FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setVisible(true);
}
OUTPUT
EXERCISE
Write a program using JPasswordField and JTextField to demonstrate the use of user
authentication
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserAuthenticationDemo extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
public UserAuthenticationDemo() {
setTitle("User Authentication Demo");
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
usernameField = new JTextField();
passwordField = new JPasswordField();
loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
char[] passwordChars = passwordField.getPassword();
String password = new String(passwordChars);
// Replace this with your authentication logic
if (authenticate(username, password)) {
JOptionPane.showMessageDialog(null, "Authentication successful!");
} else {
JOptionPane.showMessageDialog(null, "Authentication failed!");
// Clear fields after attempting login
usernameField.setText("");
passwordField.setText("");
});
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel()); // Empty label for spacing
panel.add(loginButton);
add(panel);
// Replace this with your actual authentication logic
private boolean authenticate(String username, String password) {
// Simulating authentication logic
return username.equals("admin") && password.equals("password");
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UserAuthenticationDemo().setVisible(true);
});
OUTPUT
Write a program using JTextField to perform the addition of two numbers
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NumberAdditionApp extends JFrame {
private JTextField numField1, numField2, resultField;
private JButton addButton;
public NumberAdditionApp() {
setTitle("Number Addition");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
numField1 = new JTextField(10);
numField2 = new JTextField(10);
resultField = new JTextField(10);
resultField.setEditable(false);
addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(numField1.getText());
double num2 = Double.parseDouble(numField2.getText());
double result = num1 + num2;
resultField.setText(Double.toString(result));
} catch (NumberFormatException ex) {
resultField.setText("Invalid input");
}
});
add(new JLabel("Number 1:"));
add(numField1);
add(new JLabel("Number 2:"));
add(numField2);
add(addButton);
add(new JLabel("Result:"));
add(resultField);
pack();
setLocationRelativeTo(null); // Center the frame
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NumberAdditionApp().setVisible(true);
});
OUTPUT
Write a program using JPasswordField to accept password from user and if the length is less
than 6 characters then error message should be displayed "Password length must be >>6
characters"
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PasswordFieldExample extends JFrame implements ActionListener {
// Create components
JLabel label;
JPasswordField passwordField;
JButton submitButton;
public PasswordFieldExample() {
setTitle("Password Field Example");
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
label = new JLabel("Enter Password:");
passwordField = new JPasswordField(15);
submitButton = new JButton("Submit");
// Add components to the frame
add(label);
add(passwordField);
add(submitButton);
// Add ActionListener to the button
submitButton.addActionListener(this);
setVisible(true);
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
char[] password = passwordField.getPassword();
if (password.length < 6) {
JOptionPane.showMessageDialog(this, "Password length must be >= 6 characters");
} else {
JOptionPane.showMessageDialog(this, "Password accepted!");
public static void main(String[] args) {
new PasswordFieldExample();
OUTPUT