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

Document 8

The documents demonstrate how to use key and mouse listeners in Java Swing applications. Key listeners allow detecting key presses, releases, and types in text fields. Mouse listeners allow detecting mouse clicks, presses, releases, hovering, and movement over Swing components. The examples show updating labels with listener events and counting mouse clicks.

Uploaded by

compstudent2024
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 views36 pages

Document 8

The documents demonstrate how to use key and mouse listeners in Java Swing applications. Key listeners allow detecting key presses, releases, and types in text fields. Mouse listeners allow detecting mouse clicks, presses, releases, hovering, and movement over Swing components. The examples show updating labels with listener events and counting mouse clicks.

Uploaded by

compstudent2024
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/ 36

package Practical_9;

import javax.swing.*;

public class ProgressBarExample extends JFrame

JProgressBar jb;

int i=0,num=0;

ProgressBarExample()

jb=new JProgressBar(0,2000);

jb.setBounds(40,40,160,30);

jb.setValue(0);

jb.setStringPainted(true);

add(jb);

setSize(250,150);

setLayout(null);

public void iterate()

while(i<=2000)

jb.setValue(i);

i=i+20;

try{Thread.sleep(150);}

catch(Exception e){}

public static void main(String[] args) {

ProgressBarExample m=new ProgressBarExample();

m.setVisible(true);

m.iterate();

} }
package Practical_9;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

class MyFrame extends JFrame implements ActionListener {

JProgressBar pb;

JButton b1 = new JButton("LOGIN");

MyFrame() {

setLayout(null);

pb = new JProgressBar(1, 100);

pb.setValue(0);

pb.setStringPainted(true);

b1.setBounds(20, 20, 80, 25);

pb.setBounds(110, 20, 200, 25);

pb.setVisible(false);

add(b1);

add(pb);

b1.addActionListener(this);

setResizable(false);

setDefaultCloseOperation(EXIT_ON_CLOSE);

public void actionPerformed(ActionEvent e) {

int i = 0;

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

pb.setVisible(true);

try {

while (i <= 100) {

Thread.sleep(50);

pb.paintImmediately(0, 0, 200, 25);

pb.setValue(i);

i++;
}

} catch (Exception e1) {

System.out.print("Caughted exception is =" + e1);

public class ProgressBar_Example_2 {

public static void main(String arg[]) {

MyFrame m = new MyFrame();

m.setSize(330, 100);

m.setVisible(true);

}
package Practical_10;
import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.*;

public class KeyListener_Expl_1 implements KeyListener

Frame f1;

Label l1;

TextField t1;

public static void main(String args[])

KeyListener_Expl_1 d1=new KeyListener_Expl_1();

d1.MyFrame();

void MyFrame()

f1=new Frame();

f1.setSize(500,500);

f1.setVisible(true);

f1.addWindowListener(new WindowAdapter()

public void windowClosing(WindowEvent e)

System.exit(0);

});

f1.setTitle("KeyEvent Demo");
f1.setLayout(new FlowLayout(FlowLayout.CENTER,100,10));

l1=new Label("my Label");

t1=new TextField();

t1.addKeyListener(this);

f1.add(l1);

f1.add(t1);

@Override

public void keyTyped(KeyEvent e) {

if(e.getSource()==t1)

// l1.setText("key typed");

@Override

public void keyPressed(KeyEvent e) {

// TODO Auto-generated method stub

l1.setText("Key Pressed");

@Override

public void keyReleased(KeyEvent e) {

// TODO Auto-generated method stub

// l1.setText("key Released");

}
package Practical_10;
import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.*;

public class KeyListener_Expl_2 implements KeyListener

Frame f1;

Label l1,l2,l3;

TextField t1,t2,t3;

public static void main(String args[])

KeyListener_Expl_2 d1=new KeyListener_Expl_2();

d1.MyFrame();

void MyFrame()

f1=new Frame();

f1.setSize(500,500);

f1.setVisible(true);

f1.addWindowListener(new WindowAdapter()

public void windowClosing(WindowEvent e)

System.exit(0);

});

f1.setTitle("KeyEvent Demo");

f1.setLayout(null);

l1=new Label("Enter First Number: ");


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

t1=new TextField();

t1.setBounds(260,100,60,30);

l2=new Label("Enter Second Number: ");

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

t2=new TextField();

t2.setBounds(260,150,60,30);

l3=new Label("Which Operation: ");

l3.setBounds(100,200,150,30);

t3=new TextField();

t3.setBounds(250,200,100,30);

t3.addKeyListener(this);

f1.add(l1);

f1.add(t1);

f1.add(l2);

f1.add(t2);

f1.add(l3);

f1.add(t3);

@Override

public void keyTyped(KeyEvent e) {

if(e.getSource()==t1)

// l1.setText("key typed");

@Override

public void keyPressed(KeyEvent e) {

// TODO Auto-generated method stub

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());
Integer c = a+b;

t3.setText(" = "+Integer.toString(c));

System.out.println(Integer.toString(c));

@Override

public void keyReleased(KeyEvent e) {

// TODO Auto-generated method stub

// l1.setText("key Released");

}
package Practical_11;
import java.applet.*;

import java.awt.*;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

//Write a program to change the background color of Applet when user performs

//events using Mouse

/*<html>

<head>

<applet code="MouseEvent_Expl_1.class" width="200" height="100">

</applet>

</head>

</html>*/

public class MouseEvent_Expl_1 extends Applet implements MouseListener

Button b;

TextField tf;

public void init()

b=new Button("Click");

b.setBounds(80,150,60,50);

tf=new TextField();

tf.setBounds(30,40,150,20);

tf.addMouseListener(this);

add(b);

add(tf);

setBackground(Color.cyan);

setLayout(null);

@Override

public void mouseClicked(MouseEvent e) {


setBackground(Color.red);

@Override

public void mousePressed(MouseEvent e) {

setBackground(Color.blue);

@Override

public void mouseReleased(MouseEvent e) {

setBackground(Color.pink);

@Override

public void mouseEntered(MouseEvent e) {

setBackground(Color.yellow);

@Override

public void mouseExited(MouseEvent e) {

setBackground(Color.black);

}
//Write a program to count the number of clicks performed by the user in a
Frame
//window
package Practical_11;
import java.awt.event.*;

import javax.swing.*;

public class MouseListener_Expl_2 implements MouseListener

int count = 0;

JLabel l1;

public void myFrame()

JFrame f1 = new JFrame("Mouse Listener event");

f1.setVisible(true);

f1.setBounds(100,100,800,700);

f1.setLayout(null);

f1.addMouseListener(this);

l1 = new JLabel("User clickes 0 times");

l1.setBounds(100,100,200,40);

f1.add(l1);

public static void main(String[] args)

MouseListener_Expl_2 obj = new MouseListener_Expl_2();

obj.myFrame();

@Override

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

count =count + 1;

l1.setText("User clickes "+count+" times");


}

@Override

public void mousePressed(MouseEvent e) {

@Override

public void mouseReleased(MouseEvent e) {

@Override

public void mouseEntered(MouseEvent e) {

@Override

public void mouseExited(MouseEvent e) {

}
package Practical_11;
import java.awt.event.*;

import javax.swing.*;

public class MouseListener_Expl_3 implements MouseMotionListener {

int count = 0;

JLabel l1;

public void myFrame(){

JFrame f1 = new JFrame("Mouse Listener event");

f1.setVisible(true);

f1.setBounds(100,100,800,700);

f1.setLayout(null);

f1.addMouseMotionListener(this);

l1 = new JLabel("my label");

l1.setBounds(100,100,200,40);

f1.add(l1);

public static void main(String[] args) {

MouseListener_Expl_3 obj = new MouseListener_Expl_3();

obj.myFrame();}

@Override

public void mouseDragged(MouseEvent e) {

l1.setText("Mouse Dragged");

@Override

public void mouseMoved(MouseEvent e) {

l1.setText("Mouse Moved ");} }


package Practical_12;
import javax.swing.*;

public class JPasswordField_Expl

JPasswordField_Expl()

JFrame f1 = new JFrame("Use of JPasswordField");

f1.setVisible(true);

f1.setBounds(100,100,800,700);

f1.setLayout(null);

JLabel l1 = new JLabel("Enter Password");

l1.setBounds(100,100,100,40);

f1.add(l1);

JPasswordField pf =new JPasswordField();

pf.setEchoChar('#');

pf.setBounds(200,100,150,30);

f1.add(pf);

public static void main(String[] args)

new JPasswordField_Expl();

}
package Practical_12;
import javax.swing.*;

public class JPasswordField_Expl_2 {

JPasswordField_Expl_2()

JFrame f1 = new JFrame("Use of JTextField and JPasswordField");

f1.setVisible(true);

f1.setBounds(100,100,800,700);

f1.setLayout(null);

JLabel l1 = new JLabel("Enter Username");

l1.setBounds(100,100,100,30);

f1.add(l1);

JTextField tf1 =new JTextField();

tf1.setBounds(200,100,150,30);

f1.add(tf1);

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

l2.setBounds(100,160,100,30);

f1.add(l2);

JPasswordField pf =new JPasswordField();

pf.setBounds(200,160,150,30);

f1.add(pf);

JButton b1 = new JButton("Submit");

b1.setBounds(200,220,80,30);

f1.add(b1);

public static void main(String[] args) {

new JPasswordField_Expl_2();

}
}
package Practical_12;
import java.awt.event.*;

import javax.swing.*;

public class JPasswordField_Expl_3 implements ActionListener

JLabel l3;

int a,b,c;

JTextField tf1,pf;

public JPasswordField_Expl_3()

JFrame f1 = new JFrame("Use of JTextField and JPasswordField");

f1.setVisible(true);

f1.setBounds(100,100,800,700);

f1.setLayout(null);

JLabel l1 = new JLabel("First Number");

l1.setBounds(100,100,100,30);

f1.add(l1);

tf1 =new JTextField();

tf1.setBounds(200,100,150,30);

f1.add(tf1);

JLabel l2 = new JLabel("Second Number");

l2.setBounds(100,160,100,30);

f1.add(l2);

pf =new JTextField();

pf.setBounds(200,160,150,30);

f1.add(pf);

JButton b1 = new JButton("Show Addition");

b1.setBounds(200,220,120,30);

b1.addActionListener(this);

f1.add(b1);

l3 = new JLabel("");
l3.setBounds(350,220,100,30);

f1.add(l3);

public static void main(String[] args)

new JPasswordField_Expl_3();

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

a = Integer.parseInt(tf1.getText());

b = Integer.parseInt(pf.getText());

c = a+b;

l3.setText("Addition is: "+c);

}
package Practical_12;
import java.awt.event.*;

import javax.swing.*;

public class JPasswordField_Expl_4 implements ActionListener

JLabel l1,l2;

JPasswordField pf;

JButton b1;

public JPasswordField_Expl_4()

JFrame f1 = new JFrame("Passowrd Verification");

f1.setVisible(true);

f1.setBounds(100,100,800,700);

f1.setLayout(null);

l1 = new JLabel("Enter Password");

l1.setBounds(100,100,100,40);

f1.add(l1);

pf =new JPasswordField();

pf.setBounds(200,100,150,30);

f1.add(pf);

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

l2.setBounds(400,100,200,40);

f1.add(l2);

b1 = new JButton("Show Addition");

b1.setBounds(200,220,120,30);

b1.addActionListener(this);

f1.add(b1);

public static void main(String[] args)

{
new JPasswordField_Expl_4();

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String pass = pf.getText();

if(pass.length()>6)

l2.setText("Your Password is:"+pass);

else

JOptionPane.showMessageDialog(null, "Password length must be\r\n" +

">6 characters", "ERROR", JOptionPane.ERROR_MESSAGE);

}
package Practical_13;
import java.awt.*;

import java.awt.event.*;

public class Adapter_Class_Expl

Frame f;

Adapter_Class_Expl()

f=new Frame("Window Adapter");

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {

System.exit(0);

});

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(String[] args)

new Adapter_Class_Expl();

package Practical_13;

abstract class Person

abstract void eat();

class TestAnonymousInner

{
public static void main(String args[])

Person p=new Person(){

void eat(){System.out.println("nice fruits");

};

p.eat();

}
package Practical_13;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MouseMotionAdapterExpl extends MouseMotionAdapter

Frame f;

MouseMotionAdapterExpl(){

f=new Frame("Mouse Motion Adapter");

f.addMouseMotionListener(this);

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

public void mouseDragged(MouseEvent e)

Graphics g=f.getGraphics();

g.setColor(Color.ORANGE);

g.fillOval(e.getX(),e.getY(),20,20);

public static void main(String[] args)

new MouseMotionAdapterExpl();

}
package Practical_14;
import java.io.*;

import java.net.*;

public class Exercise_1

public static void main(String[] args)

try {

InetAddress ip = InetAddress.getByName("www.asciieducation.com");

System.out.println("Host Name: " + ip.getHostName());

System.out.println("IP Address: " + ip.getHostAddress());

} catch (Exception e) {

System.out.println(e);

}
package Practical_14;
//1. Execute the following code and write the output

import java.io.*;

import java.net.*;

public class InetDemo

public static void main(String[] args)

try {

InetAddress ip = InetAddress.getByName("localhost");

System.out.println("Host Name: " + ip.getHostName());

System.out.println("IP Address: " + ip.getHostAddress());

} catch (Exception e) {

System.out.println(e);

}
package Practical_15;
import java.net.URL;

import java.net.MalformedURLException;

public class URL_Retrive

public static void main(String[] args) throws MalformedURLException

URL url = new URL("https://msbte.org.in/");

System.out.println("Authority: "+ url.getAuthority());

System.out.println("Default Port: "+ url.getDefaultPort());

System.out.println("File: "+ url.getFile());

System.out.println("Path: "+ url.getPath());

System.out.println("Protocol: "+ url.getProtocol());

System.out.println("Reference: "+ url.getRef());

}
package Practical_15;
import java.net.*;

class URLDemo1

public static void main(String args[]) throws MalformedURLException

URL hp = new URL("https://www.javatpoint.com/javafx-tutorial");

System.out.println("Protocol: " + hp.getProtocol());

System.out.println("Port: " + hp.getPort());

System.out.println("Host: " + hp.getHost());

System.out.println("File: " + hp.getFile());

System.out.println("Ext:" + hp.toExternalForm());

}
package Practical_15;
import java.net.*;

import java.util.*;

import java.io.IOException;

public class URLInfo

public static void main(String[] args) throws IOException , MalformedURLException

Scanner sc = new Scanner(System.in);

System.out.print("Enter any Url: ");

String ad = sc.nextLine();

URL url = new URL(ad);

URLConnection uc = url.openConnection();

System.out.println("Date:"+ new Date(uc.getDate()) );

System.out.println("Content Type: "+ uc.getContentType());

System.out.println("Content Length: "+ uc.getContentLength());

}
package Practical_16;

import java.net.Socket;

import java.io.*;

public class ClientSide

public static void main(String[] args) throws IOException {

Socket s = new Socket("localhost",2019);

System.out.println("Client Started, waiting for server response..");

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in)

);

OutputStream os = s.getOutputStream();

BufferedReader br1 = new BufferedReader(

new InputStreamReader(s.getInputStream())

);

PrintStream ps = new PrintStream(os);

do{

System.out.print("Client: ");

String msg = br.readLine();

ps.println(msg);

String res = br1.readLine();

System.out.println("Server Send:"+res+"\n\n");

while(true);

}
package Practical_16;

import java.net.*;

import java.io.*;

public class ServerSide{

public static void main(String[] args) throws IOException {

ServerSocket s = new ServerSocket(2019);

System.out.println("Server Started, waiting for client");

Socket s1 = s.accept();

// Client Send

BufferedReader br = new BufferedReader(

new InputStreamReader(s1.getInputStream())

);

OutputStream out = s1.getOutputStream();

PrintStream ps = new PrintStream(out);

BufferedReader br1 = new BufferedReader(

new InputStreamReader(System.in)

);

do{

String res = br.readLine();

System.out.println("Client Send: "+res);

System.out.print("Server: ");

String msg = br1.readLine();

System.out.print("\n\n");

ps.println(msg);

while(true);

}
package Practical_16;
import java.io.*;

import java.net.*;

public class SocketClientExample {

public static void main(String[] args) throws UnknownHostException, IOException,


ClassNotFoundException, InterruptedException{

InetAddress host = InetAddress.getLocalHost();

Socket socket = null;

ObjectOutputStream oos = null;

ObjectInputStream ois = null;

for(int i=0; i<5;i++){

socket = new Socket(host.getHostName(), 9876);

oos = new ObjectOutputStream(socket.getOutputStream());

System.out.println("Sending request to Socket Server");

if(i==4)oos.writeObject("exit");

else oos.writeObject(""+i);

ois = new ObjectInputStream(socket.getInputStream());

String message = (String) ois.readObject();

System.out.println("Message: " + message);

ois.close();

oos.close();

Thread.sleep(100);

}
package Practical_16;
import java.io.*;

import java.lang.ClassNotFoundException;

import java.net.*;

public class SocketServerExample {

private static ServerSocket server;

private static int port = 9876;

public static void main(String args[]) throws IOException, ClassNotFoundException{

server = new ServerSocket(port);

while(true){

System.out.println("Waiting for the client request");

Socket socket = server.accept();

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

String message = (String) ois.readObject();

System.out.println("Message Received: " + message);

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

oos.writeObject("Hi Client "+message);

ois.close();

oos.close();

socket.close();

if(message.equalsIgnoreCase("exit")) break;

System.out.println("Shutting down Socket server!!");

server.close();

}
package Practical_16;
import java.net.Socket;

import java.io.*;

public class ValidateClient

public static void main(String[] args) throws IOException {

Socket s = new Socket("localhost" , 2019);

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in)

);

System.out.print("Enter Username and Password: ");

String user = br.readLine();

String pass = br.readLine();

OutputStream os = s.getOutputStream();

PrintStream ps = new PrintStream(os);

ps.println(user);

ps.println(pass);

BufferedReader br1 = new BufferedReader(

new InputStreamReader(s.getInputStream())

);

String res = br1.readLine();

System.out.println(res);

}
package Practical_16;
import java.net.*;

import java.io.*;

public class ValidateServer

public static void main(String[] args) throws IOException

ServerSocket s = new ServerSocket(2019);

System.out.println("Server Started, waiting for client");

Socket s1 = s.accept();

BufferedReader br = new BufferedReader(

new InputStreamReader(s1.getInputStream())

);

String user = br.readLine();

String pass = br.readLine();

OutputStream out = s1.getOutputStream();

PrintStream ps = new PrintStream(out);

if(user.equals("abc") && pass.equals("1234"))

ps.println("Validate Successfully");

else

ps.println("Validate Un-Successfull");

}
package Practical_17;

import java.net.*;

import java.io.*;

public class Client_Location

public static void main(String args[])throws Exception

byte b[]=new byte[1024];

FileInputStream f=new FileInputStream("D:\\NetworkingDemo.txt");

DatagramSocket dsoc=new DatagramSocket(2000);

int i=0;

while(f.available()!=0)

b[i]=(byte)f.read();

i++;

f.close();

dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),1000));

}
package Practical_17;
import java.net.*;

public class DgramRec {

public DgramRec() {

public static void main(String[] arg) throws Exception {

DatagramSocket ds = new DatagramSocket(3000);

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, 1024);

ds.receive(dp);

String str = new String(dp.getData(), 0, dp.getLength());

System.out.println(str);

ds.close();

You might also like