Java Programming EVENT HANDLING (lecture -14)

Adapter Classes „ „ „ „

Often we only really want to implement one or two of the event handlers But interface implementation requires us to implement all of them J Java adapter d t classes l allow ll us tto solve l thi this problem bl Adapter classes Implement p the event-handling g interfaces in a trivial way, y, just j to satisfy the interface … We extend the class and override the methods that we need …

2

Adapter classes „

Some adapter classes: … KeyAdapter (instead of KeyListener) … WindowAdapter … MouseAdapter (MouseListener) … MouseMotionAdapter (MouseMotionListener) etc… etc

Adapter Classes import java.awt.*; import java.awt.event.*; class CloseFrame extends Frame{ Label abe label; abe ; CFListener w = new CFListener(this); CloseFrame(String title) { super(title); label = new Label("Close the frame."); this.addWindowListener(w); } void oid launchFrame() la nchFrame() { setSize(300,300); setVisible(true); } public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window Example"); cf.launchFrame(); }

Adapter p Classes class CFListener extends WindowAdapter { CloseFrame ref; CFListener( CloseFrame ref ){ this ref = ref; this.ref } public bli void id windowClosing(WindowEvent i d Cl i (Wi d E t e)) { ref.dispose(); System.exit(1); } }

Inner Classes Class declared within another class „ Why use inner classes? „

… Help

simplify your programs … Especially in event handling

Example import p jjava.awt.*;; import p jjava.awt.event.*;; class CloseFrame extends Frame{ Label label; CloseFrame(String title) { super(title); label = new Label("Close the frame."); this.addWindowListener(new CFListener()); } () { void launchFrame() setSize(300,300); setVisible(true);

}

class CFListener extends WindowAdapter { public void windowClosing(WindowEvent e) { dispose(); System.exit(1); } }

Example public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window Example"); cf.launchFrame(); (); } }

Anonymous Inner Class „ „

Unnamed inner classes Why use anonymous inner classes? … Further

simplify p y yyour codes … Especially in event handling

Example import java.awt.*; import java.awt.event.*; class CloseFrame extends Frame{ Label label; CloseFrame(String title) { super(title); label = new Label("Close the frame."); this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ dispose(); System.exit(1); } } ) }}\\end of constructor

Example void launchFrame() { setSize(300,300); setVisible(true); } public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close CloseFrame( Close Window Example"); Example ); cf.launchFrame(); } }

Another Style …………………

rightButton = new JButton( "Right" ); container.add(( rightButton g )); rightButton.addActionListener( new ActionListener() { // anonymous inner class // process rightButton event public void actionPerformed( ActionEvent event ) { //action to be performed } } ); …………………

Using anonymous inner classes „ „ „

This is a variation of the p previous option. p An anonymous inner class is an inner class without a name. You define the class “on the fly” when you need an instance of it (an object) to be created.

More on Inner Classes „ „ „

you can implement your listeners inside the class that extends your JFrame, JFrame making it an inner class. class This enables you to put everything in one class (and hence file). I Inner classes l have h access tto the th private i t data d t off the th outer class.

Example 1 Example-1 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // An action listener that prints a message.

public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("I Syste out p t ( was as cclicked."); c ed ); } }

Example-1 import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; // This program demonstrates how to install an action listener.

public class ButtonTester { public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); :

frame.add(button); ActionListener listener = new ClickListener(); button addActionListener(listener); button.addActionListener(listener); frame.setSize(100, 60); frame.setDefaultCloseOperation(JFrame.EXIT ON CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}

Event Handling: JCheckbox private class MyCheckBoxHandler implements ItemListener { ……….. private JCheckBox italic; italic = new JCheckBox( "Italic" ); container.add( italic ); // register listeners for JCheckBoxes CheckBoxHandler handler = new CheckBoxHandler(); italic.addItemListener( handler ); …………. public void itemStateChanged( ItemEvent event ) { // p process bold checkbox events if ( event.getSource() == italic){…..} } …………. }