0% found this document useful (0 votes)
8 views

Event handling programs

The document contains multiple Java programs demonstrating various event handling techniques using Swing, including action events, item events, key events, mouse events, and window events. Each program showcases how to respond to user interactions such as button clicks, checkbox selections, key presses, and mouse movements. The examples illustrate the implementation of interfaces like ActionListener, ItemListener, KeyListener, MouseListener, and WindowListener to handle events effectively.

Uploaded by

shreyassupe346
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)
8 views

Event handling programs

The document contains multiple Java programs demonstrating various event handling techniques using Swing, including action events, item events, key events, mouse events, and window events. Each program showcases how to respond to user interactions such as button clicks, checkbox selections, key presses, and mouse movements. The examples illustrate the implementation of interfaces like ActionListener, ItemListener, KeyListener, MouseListener, and WindowListener to handle events effectively.

Uploaded by

shreyassupe346
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/ 11

EVENT HANDLING – PROGRAMS

//Java Program to demonstrate Action Event


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

public class lab1 extends JFrame implements


ActionListener
{
JButton b1=new JButton("OK");
JButton b2=new JButton("CANCEL");
JLabel l1=new JLabel();

lab1()
{
setLayout(new FlowLayout());
add(l1);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("Action Event Demonstration");
setSize(500,600);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("OK"))
l1.setText("OK BUTTON CLICKED");
else
l1.setText("CANCEL BUTTON CLICKED");
}
public static void main(String[] args)
{
new lab1();
}
}
//Action Events getwhen() example
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import javax.swing.*;

public class Action2 extends JFrame implements


ActionListener
{
JButton b1=new JButton("OK");;
JTextField t1=new JTextField();;
JLabel l1=new JLabel();
JLabel l2=new JLabel();;

Action2()
{
l1.setBounds(50,350,60,30);
l2.setBounds(50,400,200,200);
t1.setBounds(100,100,300,60);
b1.setBounds(100,200,100,60);

setLayout(null);
add(t1);add(b1);add(l1);add(l2);
b1.addActionListener(this);
setSize(500,600);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String x;
x=t1.getText();
l1.setText(x);
long when = e.getWhen();
Date date = new Date(when);
l2.setText("The button was clicked"+date);
}
public static void main(String[] args)
{
new Action2();
}
}

//Item Event example


import javax.swing.*;
import java.awt.event.*;
public class item extends JFrame implements ItemListener
{
JCheckBox check1=new JCheckBox("check 1");
JCheckBox check2=new JCheckBox("check 2");
JLabel l=new JLabel();
public item()
{
setLayout(null);
check1.setBounds(50,50,80,100);
check2.setBounds(50,120,100,100);

check1.addItemListener( this);
check2.addItemListener( this);

add(check1);
add(check2);

l.setBounds(150,300,200,100);
add(l);

setVisible(true);
setSize(400,500);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable() == check1)
{
l.setText("clicked checkbox
1"+(e.getStateChange()==1?"State changed":"Not Changed"));
}
else if(e.getItem()==check2)
{
l.setText("clicked checkbox 2");
}
}
public static void main(String[] args )
{
new item();
}
}
//Key Event Example
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class key extends JFrame implements KeyListener


{
JTextArea t1=new JTextArea(6,10);
JLabel l1=new JLabel();
key()
{
setLayout(new FlowLayout());
add(l1);
add(t1);
t1.addKeyListener(this);
setSize(500,600);
setVisible(true);
}
public void keyTyped(KeyEvent e)
{
l1.setText("Key Typed");
}
public void keyPressed(KeyEvent e)
{
int code=e.getKeyCode();
String s=null;
switch(code)
{
case KeyEvent.VK_SHIFT : s= "SHift Key Pressed";
break;
case KeyEvent.VK_CONTROL: s= "Control Key
Pressed";
break;
case KeyEvent.VK_ALT : s= "Alt Key Pressed";
break;
}
l1.setText(s);
}
public void keyReleased(KeyEvent e)
{
l1.setText("Key Released");
}
public static void main(String[] args)
{
new key();
}
}

//Methods of Key Events


import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class key extends JFrame implements KeyListener
{
JTextArea t1=new JTextArea(6,10);
JLabel l1=new JLabel();
key()
{
setLayout(new FlowLayout());
add(l1);
add(t1);
t1.addKeyListener(this);
setSize(500,600);
setVisible(true);
}
public void keyTyped(KeyEvent e)
{
int code1=e.getKeyCode();
System.out.println("getKeyCode()"+code1);

char code2=e.getKeyChar();
System.out.println("getKeyChar()"+code2);

String s=e.getKeyModifiersText(code1);
System.out.println("getKeyModifiersText"+s);

String s1=e.getKeyText(code1);
System.out.println("getKeyText"+s1);
}
public void keyPressed(KeyEvent e)
{
int code=e.getKeyCode();
String s=null;
switch(code)
{
case KeyEvent.VK_SHIFT : s= "SHift Key Pressed";
break;
case KeyEvent.VK_CONTROL: s= "Control Key
Pressed";
break;
case KeyEvent.VK_ALT : s= "Alt Key Pressed";
break;
}
l1.setText(s);
}
public void keyReleased(KeyEvent e)
{
l1.setText("Key Released");
}
public static void main(String[] args)
{
new key();
}
}

//Mouse handling Events


import java.awt.event.*;
import javax.swing.*;
public class mouse1 extends JFrame implements
MouseListener
{
JLabel l1=new JLabel("Mouse Events Demo");
mouse1()
{
add(l1);
addMouseListener(this);
setTitle("MouseEvents Example");
setSize(500,600);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
l1.setText("Mouse Clicked at " +e.getX() + " "+
e.getY());
}
public void mouseEntered(MouseEvent e)
{
l1.setText("Mouse Entered at " +e.getX() + " "+
e.getY());
}
public void mouseExited(MouseEvent e)
{
l1.setText("Mouse Exited at " +e.getX() + " "+
e.getY());
}
public void mousePressed(MouseEvent e)
{
l1.setText("Mouse Pressed at " +e.getX() + " "+
e.getY());
}
public void mouseReleased(MouseEvent e)
{
l1.setText("Mouse Released at " +e.getX() + " "+
e.getY());
}
public static void main(String[] args)
{
new mouse1();
}
}

//Window Events example


import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import javax.swing.*;
public class window1 extends JFrame implements
WindowListener,WindowFocusListener{

window1()
{
setTitle("Window EVent Example");
setSize(500,500);
setVisible(true);
addWindowListener(this);
addWindowFocusListener(this);
}
@Override
public void windowGainedFocus(WindowEvent arg0)
{
System.out.println("Window Gained Focus\n");
}
@Override
public void windowLostFocus(WindowEvent arg0)
{
System.out.println("Window Lost Focus\n");
}
public static void main(String[] args)
{
window1 w=new window1();
}
@Override
public void windowActivated(WindowEvent arg0)
{
System.out.println("Window Activated\n");
}
@Override
public void windowClosed(WindowEvent arg0)
{
System.out.println("Window Closed\n");
}
@Override
public void windowClosing(WindowEvent arg0)
{
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent arg0)
{
System.out.println("Window DeActivated\n");
}
@Override
public void windowDeiconified(WindowEvent arg0)
{
System.out.println("Window DeIconified \n");
}
@Override
public void windowIconified(WindowEvent arg0)
{
System.out.println("Window Iconfified\n");
}
@Override
public void windowOpened(WindowEvent arg0)
{
System.out.println("Window Opened\n");
}

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