Looking for todays homework exercise - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Looking for todays homework exercise

Description:

Widget. Registered Listeners. Object A. Object B. Object C. Event. Event. Object A. Object B ... Events are not limited to GUI components, however they will be ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 10
Provided by: comp156
Category:

less

Transcript and Presenter's Notes

Title: Looking for todays homework exercise


1
Looking for todays homework exercise?
  • Return to the Teaching Page of my home page and
    you will find it under
  • Exercises and Handouts

2
Event Handling
Graphical User Interface
Event
Event
Event
Widget
Registered Listeners Object A Object B Object C
Event Listeners or Event Handlers
3
Creating Sending Events
  • Events are not limited to GUI components, however
    they will be introduced and discussed in that
    context
  • GUI components that can change state, generate
    events when a change occurs
  • JButtons ActionEvent
  • JTextField ActionEvent
  • JLists ListSelectionEvent
  • JCheckBox ItemEvent
  • plus many, many more...
  • All have a method which allows objects to
    register as an event listener
  • addxxxListener(xxxListener x)

4
Event Listeners/Handlers
  • Event listeners are objects that implement the
    appropriate interface that will allow them to
    register with an object that generates events
  • Ex JButtons
  • JButton b new JButton(Button 1)
  • Generate ActionEvents
  • addActionListener(ActionListener a)
  • Any object implementing the ActionListener
    interface can register with the JButton to be
    notified when it changes state (is pressed)
  • public interface ActionListener
  • void actionPerformed(ActionEvent e)
  • public class MyObject implements ActionListener
  • ...
  • public void actionPerformed(ActionEvent e)
  • System.out.println(Dont press the
    button!)
  • MyObject mo new MyObject()

By implementing the ActionListener interface,
MyObject becomes an ActionListener
5
Buttons
  • import javax.swing.
  • import java.awt.
  • import java.awt.event.
  • public class GuiTest
  • public GuiTest()
  • public static void main(String args)
  • JFrame j new JFrame("Title")
  • j.setSize(275, 170)
  • Container c j.getContentPane()
  • c.setLayout(new FlowLayout())
  • JButton b new JButton("I Believe")
  • b.addActionListener(new
    MyActionListener())
  • c.add(b)
  • JLabel lab new JLabel("Text and
    Graphics Label")
  • c.add(lab)

6
Adapters
  • Interfaces require you to implement ALL of the
    methods
  • As we saw with WindowEvent, there are seven
    methods associated with the WindowListener
    interface
  • Cumbersome to implement all seven if we only want
    to handle one of the event types, i.e.,
    windowClosing()
  • HenceAdapters
  • abstract classes with empty methods
  • public void windowClosing()
  • simply provided as a convenience for implementing
    interfaces
  • Use extend the abstract class and override
    desired methods
  • How do we know if there is an adapter class
    associated with the interface?

7
Buttons Revisted
  • import javax.swing.
  • import java.awt.
  • import java.awt.event.
  • public class GuiTest
  • public GuiTest()
  • public static void main(String args)
  • JFrame j new JFrame("Title")
  • j.setSize(275, 170)
  • Container c j.getContentPane()
  • c.setLayout(new FlowLayout())
  • JButton b new JButton("I Believe")
  • b.addActionListener(new
    MyActionListener())
  • c.add(b)
  • JLabel lab new JLabel("Text and
    Graphics Label")
  • c.add(lab)

8
Inner Classes
  • import javax.swing.
  • import java.awt.
  • import java.awt.event.
  • public class GuiTest extends JFrame
  • private Container c
  • public GuiTest(String t)
  • super(t)
  • setSize(275, 170)
  • c getContentPane()
  • c.setLayout(new FlowLayout())
  • JButton b new JButton("I Believe")
  • b.addActionListener(new
    MyActionListener())
  • c.add(b)
  • JLabel lab new JLabel("Text and
    Graphics Label")
  • c.add(lab)
  • setVisible(true)

9
Exercise Handout
  • See my homepage...More GUIs (12 Sep 05) under
    Exercises and Handouts
  • Text Fields, Lists and Scroll Panes
  • Adding additional function to your listeners
  • Complete before tomorrows lab
  • GUI reference Deitel Chapters 11 and 22
Write a Comment
User Comments (0)
About PowerShow.com