Handling GUI Generated Events 3

Developing Event Listener:-

In this section, you will learn about a design and implementation choices for implementing event listeners.

Event Adapters:-

  • It is a lot of work to implement all of the methods in each of the listener interfaces, particularly the MouseListener interface and WindowListener interface.
  • For example, the MouseListener interface declares the following methods:

public void mouseClicked(MouseEvent event)

public void mouseEntered(MouseEvent event)

public void mouseExited(MouseEvent event)

public void mousePressed(MouseEvent event)

public void mouseReleased(MouseEvent event)

  • As a convenience, the java programming language provides adapter classes that implement each interface containing more than one method. The methods in these adapter classes are empty.
  • You can extend an adapter class and override only those methods that you need.

  1. import javax.swing.*;
  2. import java.awt.event;
  3. import java.awt.*;
  4. public class MouseClickedHandler extends MouseAdapter
  5. {
  6. // we just need the mouseClick handler, so we use the adapter to avoid
  7. // having to write all the event handler methods.
  8. public void mouseClicked(MouseEvent e)
  9. {
  10. // do stuff with the mouse click…
  11. }
  12. }

Event Handling Using Inner Classes:-

The code given below shows how to create event handler as an inner class. Using inner classes for event handlers gives you access to the private data of the outer class.

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. public class TestInner
  5. {
  6. private JFrame f;
  7. private JTextField tf;
  8. public TestInner()
  9. {
  10. f = new JFrame(“Inner classes example”);
  11. tf = new JTextField(30);
  12. }
  13. class MyMouseMotionListener extends MouseMotionAdapter
  14. {
  15. public void mouseDragged(MouseEvent e)
  16. {
  17. String s = “Mouse dragging: X = ” + e.getX() + “Y = ” + e.getY();
  18. tf.setText(s);
  19. }
  20. }
  21. public void launchFrame()
  22. {
  23. JLabel label = new JLabel(“Click and drag the mouse”);
  24. // add components to the frame
  25. f.add(label, BorderLayout.NORTH);
  26. f.add(tf, BorderLayout.SOUTH);
  27. // add a listener that uses an Inner class
  28. f.addMouseMotionListener(new MyMouseMotionListener());
  29. f.addMouseListener(new MouseListener());
  30. // size the frame and make it visible
  31. f.setSize(300, 200);
  32. f.setVisible(true);
  33. }
  34. public static void main(String[] args)
  35. {
  36. TestInner obj = new TestInner();
  37. obj.launchFrame();
  38. }
  39. }

 

Handling GUI generated Events

 

Event Handling Using an Anonymous Class:-

  • You can include an entire class definition within the scope of an expression. This approach defines what is called an anonymous inner class and creates the instance all at one time.
  • Anonymous inner classes are often used in event handling. The code given below shows an example:

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. public class TestAnonymous
  5. {
  6. private JFrame f;
  7. private JTextField tf;
  8. public TestAnonymous()
  9. {
  10. f = new JFrame(“Anonymous classes example”);
  11. tf = new JTextField(30);
  12. }
  13. public void launchFrame()
  14. {
  15. JLabel label = new JLabel(“Click and drag the mouse”);
  16. // add components to the frame
  17. f.add(label, BorderLayout.NORTH);
  18. f.add(tf, BorderLayout.SOUTH);
  19. // add a listener that uses an anonymous class
  20. f.addMouseMotionListener(new MouseMotionAdapter()
  21. {
  22. public void mouseDragged(MouseEvent e)
  23. {
  24. String s = “Mouse dragging: X = ” + e.getX() + “ Y = ” + e.getY();
  25. tf.setText(s);
  26. }
  27. }); // <- note the closing parenthesis
  28. f.addMouseListener(new MouseClickHandler()); // not shown size the frame
  29. // and make it visible
  30. f.setSize(300, 200);
  31. f.setVisible(true);
  32. }
  33. public static void main(String[] args)
  34. {
  35. TestAnonymous obj = new TestAnonymous();
  36. obj.launchFrame();
  37. }
  38. }

Note: – The compilation of an anonymous class generates a file, such as TestAnonymous$1.class.

Concurrency in Swing:-

Applications that contain a GUI require several threads for handling the GUI efficiently.

  • Threads responsible for executing the application code are called current threads.
  • Threads responsible for handling the events generated by various components are called event dispatch threads.
  • Threads responsible for executing lengthy tasks are called worker threads. Some examples of these lengthy tasks include waiting for some shared resource, waiting for user input, blocking for network or disk I/O, performing either CPU or memory intensive calculations.
  • These tasks can be executed in the background without affecting the performance of the GUI.

You can use instance of the SwingWorker class to represent these worker threads. The SwingWorker class extends the Object class and implements the RunnableFuture interface.

The SwingWorker class provides the following utility methods:

  • For communication and coordination between worker thread tasks and tasks on other threads, the SwingWorker class provides properties such as progress and state to support inter-thread communication.
  • To execute simple background tasks, the deInBackground method can be used for running the tasks background.
  • To execute tasks that have intermediate results, the results are published in a GUI using publish and process methods.
  • To channel the background threads, they can be canceled using the cancel method.

Note: – A full description of the SwingWorker class is outside the scope of this module.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply