Chapter 5
Chapter 5
Chapter 5
ActionEvent ActionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
Steps to perform Event Handling
• Following steps are required to perform event handling:
– Register the component with the Listener
– Event handling
• Registration Methods
• For registering the component with the Listener, many
classes provide the registration methods. For example:
• Button
– public void addActionListener(ActionListener a){}
• MenuItem
– public void addActionListener(ActionListener a){}
• TextField
– public void addActionListener(ActionListener a){}
– public void addTextListener(TextListener a){}
• TextArea
– public void addTextListener(TextListener a){}
• Checkbox
– public void addItemListener(ItemListener a){}
• Choice
– public void addItemListener(ItemListener a){}
• List
– public void addActionListener(ActionListener a){}
– public void addItemListener(ItemListener a){}
Java Event Handling Code
• We can put the event handling code into one of the following
places:
– Within class
– Other class
– Anonymous class
Java event handling by implementing
ActionListener (within class)
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
//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
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
} }
Java event handling by outer class
import java.awt.*;
import java.awt.event.*;
class AEvent2 extends Frame{
TextField tf;
AEvent2(){
//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
Outer o=new Outer(this);
b.addActionListener(o);//passing outer class instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent2();
}
}
//separate file Outer.java
import java.awt.event.*;
class Outer implements ActionListener{
AEvent2 obj;
Outer(AEvent2 obj){
this.obj=obj;
}
public void actionPerformed(ActionEvent e){
obj.tf.setText("welcome");
}
}
Java event handling by anonymous class
import java.awt.*;
import java.awt.event.*;
class AEvent3 extends Frame{
TextField tf;
AEvent3(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(50,120,80,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("hello");
}
});
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent3();
}
}
Java Swing
• Is a part of Java Foundation Classes (JFC) that is used to create
window-based applications.
• JFC are a set of GUI components which simplify the development of
desktop applications.
• It is built on the top of AWT (Abstract Windowing Toolkit) API and
entirely written in java.
• Unlike AWT, Java Swing provides platform-independent and
lightweight components.
• The javax.swing package provides classes for java swing API such as
JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
Difference between AWT and Swing
No. Java AWT Java Swing
3) AWT doesn't support pluggable look Swing supports pluggable look and feel.
and feel.
1 Panel()
Creates a new panel using the default layout manager.
2 Panel(LayoutManager layout)
Creates a new panel with the specified layout manager.
Example:
• the following example is for calculating price for items. The
figure below shows the GUI of the program.
• To create the following form, we have created two panels, p1 and
p2.
• P1 have gridlayout with five rows and 2 columns whereas p2 is set
to have flowlayout.
• The textarea is added to p2 panel and the rest of the components
are on p1.
• Our frame has borderlayout manager therefore that is why we have
p1 in west and p2 east side of the frame.
• The following is the code for the layout manager with action
listeners set to calculate the prices
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalcPrice extends JFrame implements ActionListener{
JPanel p1,p2;
JLabel itemname,itemprice,qty,totalprice;
JButton calc,add;
JTextField txtitemname,txtitemprice,txtqty,txttotalprice;
JTextArea report;
double total=0,sum=0;
public CalcPrice(){
super("Sales");
p1=new JPanel(new GridLayout(5,2,6,6));
p2=new JPanel(new FlowLayout());
itemname=new JLabel("Item Name");
itemprice=new JLabel("Item Price");
qty=new JLabel("Quantity ");
totalprice=new JLabel("Total Price");
calc=new JButton("Calculate");
calc.addActionListener(this);
add=new JButton("Add");
add.addActionListener(this);
txtitemname=new JTextField("");
txtitemprice=new JTextField("");
txtqty=new JTextField("");
txttotalprice=new JTextField("");
report=new JTextArea("Item --List --Pricre \n",15,15);
report.setEditable(false);
report.setBackground(Color.lightGray);
report.setForeground(Color.red);
report.setFont(new Font( "SansSerif", Font.BOLD, 14));
p1.add(itemname);
p1.add(txtitemname);
p1.add(itemprice);
p1.add(txtitemprice);
p1.add(qty);
p1.add(txtqty);
p1.add(totalprice);
p1.add(txttotalprice);
p1.add(calc);
p1.add(add);
p2.add(report);
add(p1,BorderLayout.WEST);
add(p2,BorderLayout.EAST);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==add &&
txtitemname.getText().equals("")&txtitemprice.getText().equals("")&&txtqty.getText().equals(""))
JOptionPane.showMessageDialog(null,"Please insert
data","Error",JOptionPane.WARNING_MESSAGE);
else if(e.getSource()==add){
sum=Double.parseDouble(txtqty.getText())*Double.parseDouble(txtitemprice.getText());
total=total+sum;
report.append(txtitemname.getText()+"---"+txtqty.getText()+"X"+txtitemprice.getText()+"---
"+sum+"\n");
txtitemname.setText("");
txtitemprice.setText("");
txtqty.setText("");
}
else if (e.getSource()==calc){
report.append("Total--------"+total);
txttotalprice.setText(""+total);
}
else
JOptionPane.showMessageDialog(null,"invalid operation");
}
public static void main(String args[]){
CalcPrice calobj=new CalcPrice();
calobj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calobj.setSize(400,400);
calobj.setVisible(true);
}}