Practical 12: Write a program to demonstrate the use of JTextField and JPasswordField using Listener Interface

1) Write a program using JPasswordField to set the password character as '#' instead of '*'

import javax.swing.*;
import java.awt.*;

public class JPasswordChange
{
    public static void main(String[] args) {
        JFrame f = new JFrame();

        f.setVisible(true);
        f.setSize(400,400);
        f.setLayout(new FlowLayout());

        JPasswordField pf = new JPasswordField(20);

        pf.setEchoChar('#');

        f.add(pf);
    }

}


Output:
JPasswordField char change

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

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTextAdd implements ActionListener
{
    JTextField tf , tf1 ;
    JLabel res;

    JTextAdd()
    {
        JFrame f = new JFrame();

        f.setVisible(true);
        f.setSize(400,400);
        f.setLayout(new FlowLayout());

        JLabel jl = new JLabel("Enter 1st Number:");    
         tf = new JTextField(5);
        JLabel jl1 = new JLabel("Enter 2nd Number:");
         tf1 = new JTextField(5);
         res = new JLabel("Addition");

        tf1.addActionListener(this);

        f.add(jl);
        f.add(tf);
        f.add(jl1);
        f.add(tf1);
        f.add(res);

    }
    public static void main(String[] args) {
        JTextAdd jt = new JTextAdd();
    }

    public void actionPerformed(ActionEvent ae)
    {
        String str1 = tf.getText();
        double fn = Double.parseDouble(str1);
        double sn = Double.parseDouble(tf1.getText());
   
        res.setText("Sum is " + (fn+sn));
    }
}

Output:

Addition of 2 number using JTextField

Previous
Next Post »