mit aiti 2003 lecture 17. swing - part ii. the java event model up until now, we have focused on...

19
MIT AITI 2003 MIT AITI 2003 Lecture 17. Lecture 17. Swing - Swing - Part II Part II

Upload: cecilia-norris

Post on 31-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

MIT AITI 2003MIT AITI 2003Lecture 17. Lecture 17. Swing - Part IISwing - Part II

Page 2: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

The Java Event ModelThe Java Event Model

Up until now, we have focused on GUI's to present Up until now, we have focused on GUI's to present information (with one exception)information (with one exception)

How do GUIs How do GUIs interactinteract with users? How do applications with users? How do applications recognize when the user has done something?recognize when the user has done something?

In Java this depends on 3 related concepts:In Java this depends on 3 related concepts:– events: objects that represent a user action with the systemevents: objects that represent a user action with the system

– event sources: in Swing, these are components that can recognize event sources: in Swing, these are components that can recognize user action, like a button or an editable text fielduser action, like a button or an editable text field

– event listeners: objects that can respond when an event occursevent listeners: objects that can respond when an event occurs

Page 3: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

Event SourcesEvent Sources

Event sources can generate events.Event sources can generate events. The ones you will be most interested are subclases of The ones you will be most interested are subclases of JComponentsJComponents like like JButtonsJButtons and and JPanelsJPanels

You find out the kind of events they can generate by You find out the kind of events they can generate by reading the Javadocreading the Javadoc

Page 4: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

Event ListenersEvent Listeners

An object becomes an event listener when its class An object becomes an event listener when its class implements an event listener interface. For example:implements an event listener interface. For example:

public interface ActionListenerpublic interface ActionListener

extends EventListener {extends EventListener {

public void actionPerformed(ActionEvent e);public void actionPerformed(ActionEvent e);

}}

The event listener gets called when the event occurs if we The event listener gets called when the event occurs if we register the event listener with the event sourceregister the event listener with the event source

Page 5: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

EventsEvents

Events are instances of simple classes that supply Events are instances of simple classes that supply information about what happened.information about what happened.

For example, instances of For example, instances of MouseEventMouseEvent have have getX()getX() and and getY()getY() methods that will tell you where the mouse methods that will tell you where the mouse event (e.g., mouse press) occurred.event (e.g., mouse press) occurred.

All event listener methods take an event as an argument.All event listener methods take an event as an argument.

Page 6: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

How do I Set Up to Receive an Event?How do I Set Up to Receive an Event?

1.1. Figure out what type of event you are interested Figure out what type of event you are interested in and what component it comes from.in and what component it comes from.

2.2. Decide which object is going to Decide which object is going to handlehandle (act on) (act on) the event.the event.

3.3. Determine the correct listener interface for the Determine the correct listener interface for the type of event you are interested in.type of event you are interested in.

4.4. Write the appropriate listener method(s) for the Write the appropriate listener method(s) for the class of the handler object.class of the handler object.

5.5. Use an Use an addaddEventTypeEventTypeListener()Listener() method to method to register the listener with the event sourceregister the listener with the event source

Page 7: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

A Final Example – A Final Example – SwingApplicationSwingApplication

Page 8: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

Step-by-step GuideStep-by-step Guide

Setting up the top-level containerSetting up the top-level container Setting up buttons and labelsSetting up buttons and labels Adding components to containersAdding components to containers Handling eventsHandling events

Page 9: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

Four Swing components:Four Swing components:

                  A A frameframe ( (JFrameJFrame)). The frame is a . The frame is a top-level containertop-level container. It provides . It provides a place for other Swing components to paint themselves. The other a place for other Swing components to paint themselves. The other commonly used top-level containers are dialogs (commonly used top-level containers are dialogs (JDialogJDialog) and applets ) and applets ((JAppletJApplet).).

                  A A panelpanel ( (JPanelJPanel).). The panel is an The panel is an intermediate containerintermediate container. Its only . Its only purpose is to simplify the positioning of the button and label. Other purpose is to simplify the positioning of the button and label. Other intermediate Swing containers include intermediate Swing containers include JScrollPaneJScrollPane (scroll panes) and (scroll panes) and JTabbedPaneJTabbedPane (tabbed panes) (tabbed panes)

                  A button (A button (JButtonJButton) and a label () and a label (JLabelJLabel)). The button and label . The button and label are are atomic componentsatomic components -- components that exist not to hold other Swing -- components that exist not to hold other Swing components, but to interface with the user. The Swing API provides components, but to interface with the user. The Swing API provides many atomic components, including combo boxes (many atomic components, including combo boxes (JComboBoxJComboBox), text ), text fields (fields (JTextFieldJTextField), and tables (), and tables (JTableJTable).).

Page 10: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

Containment Hierarchy Containment Hierarchy

Page 11: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

1. Setting up the top-level 1. Setting up the top-level containercontainer

//Create the top-level container titled //Create the top-level container titled “SwingApplicatoin” “SwingApplicatoin”

JFrame frame = new JFrame frame = new JFrame("SwingApplication");JFrame("SwingApplication"); .................. ..................

frame.pack(); frame.pack(); frame.setVisible(true);frame.setVisible(true);    setDefaultCloseOperation( EXIT_ON_CLOSE setDefaultCloseOperation( EXIT_ON_CLOSE

););

Page 12: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

2. Setting up buttons and labels 2. Setting up buttons and labels 

//Create a button //Create a button JButton button = new JButton("I'm a Swing JButton button = new JButton("I'm a Swing

button!");button!"); //Create a label //Create a label JLabel label = new JLabel(“Number of button JLabel label = new JLabel(“Number of button

clicks: “ + "0 ");clicks: “ + "0 "); //Set the label text //Set the label text int numClicks = 0; int numClicks = 0; label.setText(“Number of button clicks: “ + label.setText(“Number of button clicks: “ +

numClicks);numClicks);

Page 13: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

3. Adding components to 3. Adding components to containers containers 

JPanel pane = new JPanel(); JPanel pane = new JPanel(); pane.setLayout(new GridLayout(0, pane.setLayout(new GridLayout(0, 1)); pane.add(button); 1)); pane.add(button); pane.add(label); pane.add(label); frame.getContentPane().add(pane, frame.getContentPane().add(pane, BorderLayout.CENTER);BorderLayout.CENTER);

Page 14: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

Recap: GUI part of the ExampleRecap: GUI part of the Exampleimport javax.swing.*; import java.awt.*; import java.awt.event.*;import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class SwingApplication extends JFrame{ public class SwingApplication extends JFrame{

int numClicks = 0; int numClicks = 0; JPanel pane = new JPanel(); JPanel pane = new JPanel(); JLabel label = new JLabel(“Number of button clicks: “ + numClicks); JLabel label = new JLabel(“Number of button clicks: “ + numClicks); JButton button = new JButton("I'm a Swing button!");JButton button = new JButton("I'm a Swing button!");

//Constructor //Constructor SwingApplication () { SwingApplication () {

super (“SwingApplication”); //set the frame title super (“SwingApplication”); //set the frame title pane.setLayout(new GridLayout(0, 1)); pane.setLayout(new GridLayout(0, 1)); pane.add(button); pane.add(button); pane.add(label); pane.add(label); this.getContentPane().add(pane, BorderLayout.CENTER); }  this.getContentPane().add(pane, BorderLayout.CENTER); } 

public static void main(String[] args) { public static void main(String[] args) {

SwingApplication app = new SwingApplication(); SwingApplication app = new SwingApplication(); app.pack(); app.pack(); app.setVisible(true); }app.setVisible(true); }

}}    

Page 15: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

4. Handling Events4. Handling Eventsa. 3 Key components of Event Handling Processa. 3 Key components of Event Handling Process

Event (click a button, press a key, etc.) Event (click a button, press a key, etc.)

Listener interface(ActionListener, Listener interface(ActionListener, WindowListner, etc.) WindowListner, etc.)

Object (button, frame, textfield, etc.) who is Object (button, frame, textfield, etc.) who is “listening to” the event “listening to” the event

Page 16: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

4. Handling Events4. Handling Eventsb. 3 Steps to Implement an Event Handlerb. 3 Steps to Implement an Event Handler

1.1. Implement a listener interface: Implement a listener interface: public class MyClass public class MyClass implements implements

ActionListenerActionListener  

2.2. Add the listener to an object: Add the listener to an object: button.addActionListener(this)button.addActionListener(this)  

3.3. Define the methods of the listener interface: Define the methods of the listener interface: public void actionPerformed(ActionEvent e)public void actionPerformed(ActionEvent e)

{ { ...//code that reacts to the action.....//code that reacts to the action..}}  

Page 17: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

4. Handling Events4. Handling Eventsc. SwingApplication Examplec. SwingApplication Example

Event: Event: ActionEventActionEvent Object: Object: buttonbutton Interface: Interface: ActionListenerActionListener with the with the

method method actionPerformedactionPerformed

Page 18: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

4. Handling Events4. Handling Eventsc. SwingApplication Examplec. SwingApplication Example

1.1. Implement a listener interface: Implement a listener interface: – public class SwingApplication extends JFrame public class SwingApplication extends JFrame

implements ActionListenerimplements ActionListener

2.2. Add the listener to the button (after it is created) Add the listener to the button (after it is created) – button.addActionListener(this);button.addActionListener(this);  

3.3. Define the methods of the listener interface: Define the methods of the listener interface: – Public void actionPerformed (ActionEvent e){ Public void actionPerformed (ActionEvent e){

numClicks ++; numClicks ++; label.setText (“Number of button label.setText (“Number of button

clicks: “ + numClicks); }clicks: “ + numClicks); }

Page 19: MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up

After 2 clicks ….After 2 clicks ….