Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
actionPerformed() method
The actionPerformed() method is invoked automatically whenever you click on the registered
component.
public abstract void actionPerformed(ActionEvent e);
Action event Action listener program
import java.awt.*;
import java.awt.event.*;
public class large extends Frame implements ActionListener
{
Frame f=new Frame();
Label l=new Label("1st number");
Label l1=new Label("2st number");
TextField t1=new TextField(30);
TextField t2=new TextField(30);
TextField t3=new TextField(30);
Button b=new Button("LARGEST");
large(){
f.add(l);
f.add(t1);
f.add(l1);
f.add(t2);
f.add(b);
f.add(t3);
b.addActionListener(this);
//f.setLayout(new GridLayout(3,3));
f.setLayout(new FlowLayout());
f.setVisible(true);
f.setSize(500,500);
}
public void actionPerformed(ActionEvent ae)
{
int no1=Integer.parseInt(t1.getText());
int no2=Integer.parseInt(t2.getText());
if(no1>no2){
t3.setText("FIRST NUMBER is GREATER");
}
else{
t3.setText("SECOND NUMBER is GREATER");
}
}
public static void main(String args[])
{
new large();
}
}
2. Java MouseListener Interface
The Java MouseListener is notified whenever you change the state of mouse. It is notified
against MouseEvent. The MouseListener interface is found in java.awt.event package. It has five
methods.
The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified
against MouseEvent. The MouseMotionListener interface is found in java.awt.event package. It
has two methods.
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
}
// override MouseListener five abstract methods
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Released";
repaint();
}
public void mouseClicked(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Exited";
repaint();
}
// override MouseMotionListener two abstract methods
public void mouseMoved(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Moved";
repaint();
}
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse dragged";
repaint();
}
public void paint(Graphics g)
{
// g.setFont(new Font("Monospaced", Font.BOLD, 20));
//g.fillOval(x, y, 10, 10); // gives the bullet
g.drawString(x + "," + y, x+10, y -10); // displays the x and y position
g.drawString(str, x+10, y+20); // displays the action performed
}
}
/*<applet code=MouseXY width=500 height=500></applet>*/
Class declaration
Following is the declaration for java.awt.event.KeyEvent class:
KeyListener
import java.awt.*;
import java.awt.event.*;
public class KeyListenerExample extends Frame implements KeyListener{
Label l;
TextArea area;
KeyListenerExample(){
l=new Label();
l.setBounds(20,50,100,20);
area=new TextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
l.setText("Key Pressed");
}
public void keyReleased(KeyEvent e) {
l.setText("Key Released");
}
public void keyTyped(KeyEvent e) {
l.setText("Key Typed");
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="Key" width=300 height=400> </applet>*/
public class Key extends Applet implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents--->";
public void init()
{
addKeyListener(this);
requestFocus(); /*As for requestFocus(),
this method is used to make the component get input focus.
This means that if you press any kind of key or give any input, the input is heard by the
respective Listener for that component.*/
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();//int getKeyCode():Returns the integer keyCode associated with
the key in this event.
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();//char getKeyChar(): Returns the character associated with the key
in this event.
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
8. Java ItemListener Interface
The Java ItemListener is notified whenever you click on the checkbox. It is notified against
ItemEvent. The ItemListener interface is found in java.awt.event package. It has only one
method: itemStateChanged().
itemStateChanged() method
The itemStateChanged() method is invoked automatically whenever you click or unclick on the
registered checkbox component.
ItemListerrner program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletItemEvents extends Applet implements ItemListener
{
Checkbox c1,c2,r1,r2;
CheckboxGroup cbg;
Label l1,l2;
String msg1 = "", msg2 = "";
public void init()
{
l1 = new Label("Languages you know:");
l2 = new Label("Gender:");
c1 = new Checkbox("English");
c2 = new Checkbox("French");
cbg = new CheckboxGroup();
r1 = new Checkbox("Male",cbg,true);
r2 = new Checkbox("Female",cbg,false);
c1.addItemListener(this);
c2.addItemListener(this);
r1.addItemListener(this);
r2.addItemListener(this);
add(l1);
add(c1);
add(c2);
add(l2);
add(r1);
add(r2);
}
public void itemStateChanged(ItemEvent ie)
{
String t1 = c1.getState()"English ":"";
String t2 = c2.getState()"French":"";
msg1 = "Languages:" + t1 + t2 ;
String t3 = r1.getState()"Male":"Female";
msg2 = "gender:" + t3;
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg1, 50,200);
g.drawString(msg2, 50,240);
}
}
/*
<applet code="AppletItemEvents" width="300" height="300"></applet>
*/
import java.awt.*;
import java.awt.event.*;
public class ItemListenerExample implements ItemListener{
Checkbox checkBox1,checkBox2;
Label label;
ItemListenerExample(){
Frame f= new Frame("CheckBox Example");
label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
checkBox1 = new Checkbox("C++");
checkBox1.setBounds(100,100, 50,50);
checkBox2 = new Checkbox("Java");
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1); f.add(checkBox2); f.add(label);
checkBox1.addItemListener(this);
checkBox2.addItemListener(this);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==checkBox1)
label.setText("C++ Checkbox: "
+ (e.getStateChange()==1?"checked":"unchecked"));
if(e.getSource()==checkBox2)
label.setText("Java Checkbox: "
+ (e.getStateChange()==1?"checked":"unchecked"));
}
public static void main(String args[])
{
new ItemListenerExample();
}
}
9. Java WindowListener Interface
The Java WindowListener is notified whenever you change the state of window. It is notified
against WindowEvent. The WindowListener interface is found in java.awt.event package. It has
three methods.
setSize(400,400);
setLayout(null);
setVisible(true);
}
Field
Following are the fields for java.awt.Component class:
static int ADJUSTMENT_FIRST -- Marks the first integer id for the range of adjustment
event ids.
static int ADJUSTMENT_LAST -- Marks the last integer id for the range of adjustment
event ids.
static int ADJUSTMENT_VALUE_CHANGED -- The adjustment value changed event.
static int BLOCK_DECREMENT -- The block decrement adjustment type.
static int BLOCK_INCREMENT -- The block increment adjustment type.
static int TRACK -- The absolute tracking adjustment type.
static int UNIT_DECREMENT -- The unit decrement adjustment type.
static int UNIT_INCREMENT -- The unit increment adjustment type.
Class constructors
S.N. Constructor & Description
1 Adjustable getAdjustable()
Returns the Adjustable object where this event originated.
2 int getAdjustmentType()
Returns the type of adjustment which caused the value changed event.
3 int getValue()
Returns the current value in the adjustment event.
4 boolean getValueIsAdjusting()
Returns true if this is one of multiple adjustment events.
5 String paramString()
Returns a string representing the state of this Event.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code=AScrollbar width=500 height=500></applet>*/
public class AScrollbar extends Applet implements AdjustmentListener {
public void init() {
scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 0, 0, 100);
add(scrollbar);
scrollbar.addAdjustmentListener(this);
textField = new TextField(3);
textField.setEditable(false);
add(textField);
}
Scrollbar scrollbar;
TextField textField;
}
Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
Constructs a new scroll bar with the specified orientation, initial value, visible amount, and
minimum and maximum values.
*/
import java.awt.*;
import java.awt.event.*;
class MyScroll extends Frame implements AdjustmentListener
{
Scrollbar scr_red,scr_green,scr_blue;
int r,g,b;
public MyScroll()
{
setTitle("Scrollbar");
setSize(500,500);
setLocation(100,100);
scr_red=new Scrollbar(Scrollbar.HORIZONTAL,0,45,0,300);
scr_green=new Scrollbar(Scrollbar.HORIZONTAL,0,45,0,300);
scr_blue=new Scrollbar(Scrollbar.HORIZONTAL,0,45,0,300);
scr_red.addAdjustmentListener(this);
scr_green.addAdjustmentListener(this);
scr_blue.addAdjustmentListener(this);
setLayout(null);
//setLayout(new FlowLayout());
setVisible(true);
scr_red.setBounds(10,50,200,20);
scr_green.setBounds(10,80,200,20);
scr_blue.setBounds(10,110,200,20);
add(scr_red);
add(scr_green);
add(scr_blue);
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
if(ae.getSource()==scr_red)
{
r=scr_red.getValue();
}
else if(ae.getSource()==scr_green)
{
g=scr_green.getValue();
}
else if(ae.getSource()==scr_blue)
{
b=scr_blue.getValue();
}
setBackground(new Color(r,g,b));
}
public static void main(String []args)
{
new MyScroll();
}
}
Go through this link for more details
https://www.decodejava.com/java-events.htm
11. Java TextListener
The class which processes the TextEvent should implement this interface.The object of that
class must be registered with a component. The object can be registered using the
addTextListener() method.
Interface declaration
Following is the declaration for java.awt.event.TextListener interface:
Interface methods
S.N. Method & Description
1 void textValueChanged(TextEvent e)
Invoked when the value of the text has changed.
import java.awt.*;
import java.awt.event.*;
TextEventEx1()
{
label1= new Label("Type in the textfield, to see the textevents it generates -", Label.CENTER);
label2= new Label();
field1 = new TextField(25);
jf.setLayout(new FlowLayout());
jf.add(label1);
jf.add(field1);
jf.add(label2);
//Registering the class TextEventEx1 to catch and respond to mouse text events
field1.addTextListener(this);
jf.setSize(340,200);
jf.setVisible(true);
}
Interface methods
S.N. Method & Description
1 void componentHidden(ComponentEvent e)
Invoked when the component has been made invisible.
2 void componentMoved(ComponentEvent e)
Invoked when the component's position changes.
3 void componentResized(ComponentEvent e)
Invoked when the component's size changes.
4 void componentShown(ComponentEvent e)
Invoked when the component has been made visible.
import java.awt.*;
import java.awt.event.*;
public class Example extends Frame implements ComponentListener {
public static void main(String args[]) {
dump("Hidden", event);
dump("Moved", event);
dump("Resized", event);
dump("Shown", event);
};
lbutton.setName("Left");
lbutton.addComponentListener(this);
lright.addComponentListener(this);
lright.setVisible(!lright.isVisible());
};
lbutton.addActionListener(action);
lbutton, lright);
cPane.add(splitBar, BorderLayout.CENTER);
f.setSize(500, 400);
f.setVisible(true);
}
}