Download presentation
Presentation is loading. Please wait.
Published byCecilia Norris Modified over 9 years ago
1
MIT AITI 2003 Lecture 17. Swing - Part II
2
The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up until now, we have focused on GUI's to present information (with one exception) How do GUIs interact with users? How do applications recognize when the user has done something? How do GUIs interact with users? How do applications 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 system –event sources: in Swing, these are components that can recognize user action, like a button or an editable text field –event listeners: objects that can respond when an event occurs
3
Event Sources Event sources can generate events. Event sources can generate events. The ones you will be most interested are subclases of JComponents like JButtons and JPanels The ones you will be most interested are subclases of JComponents like JButtons and JPanels You find out the kind of events they can generate by reading the Javadoc You find out the kind of events they can generate by reading the Javadoc
4
Event Listeners An object becomes an event listener when its class implements an event listener interface. For example: An object becomes an event listener when its class implements an event listener interface. For example: public 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 register the event listener with the event source The event listener gets called when the event occurs if we register the event listener with the event source
5
Events Events are instances of simple classes that supply information about what happened. Events are instances of simple classes that supply information about what happened. For example, instances of MouseEvent have getX() and getY() methods that will tell you where the mouse event (e.g., mouse press) occurred. For example, instances of MouseEvent have getX() and getY() methods that will tell you where the mouse 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.
6
How do I Set Up to Receive an Event? 1. Figure out what type of event you are interested in and what component it comes from. 2. Decide which object is going to handle (act on) the event. 3. Determine the correct listener interface for the type of event you are interested in. 4. Write the appropriate listener method(s) for the class of the handler object. 5. Use an add EventType Listener() method to register the listener with the event source
7
A Final Example – SwingApplication
8
Step-by-step Guide Setting up the top-level container Setting up the top-level container Setting up buttons and labels Setting up buttons and labels Adding components to containers Adding components to containers Handling events Handling events
9
Four Swing components: A frame ( JFrame ). The frame is a top-level container. It provides a place for other Swing components to paint themselves. The other commonly used top-level containers are dialogs ( JDialog ) and applets ( JApplet ). A frame ( JFrame ). The frame is a top-level container. It provides a place for other Swing components to paint themselves. The other commonly used top-level containers are dialogs ( JDialog ) and applets ( JApplet ). A panel ( JPanel ). The panel is an intermediate container. Its only purpose is to simplify the positioning of the button and label. Other intermediate Swing containers include JScrollPane (scroll panes) and JTabbedPane (tabbed panes) A panel ( JPanel ). The panel is an intermediate container. Its only purpose is to simplify the positioning of the button and label. Other intermediate Swing containers include JScrollPane (scroll panes) and JTabbedPane (tabbed panes) A button ( JButton ) and a label ( JLabel ). The button and label are atomic components -- components that exist not to hold other Swing components, but to interface with the user. The Swing API provides many atomic components, including combo boxes ( JComboBox ), text fields ( JTextField ), and tables ( JTable ). A button ( JButton ) and a label ( JLabel ). The button and label are atomic components -- components that exist not to hold other Swing components, but to interface with the user. The Swing API provides many atomic components, including combo boxes ( JComboBox ), text fields ( JTextField ), and tables ( JTable ).
10
Containment Hierarchy
11
1. Setting up the top-level container //Create the top-level container titled “SwingApplicatoin” //Create the top-level container titled “SwingApplicatoin” JFrame frame = new JFrame("SwingApplication");.................. JFrame frame = new JFrame("SwingApplication");.................. frame.pack(); frame.pack(); frame.setVisible(true); frame.setVisible(true); setDefaultCloseOperation( EXIT_ON_CLOSE ); setDefaultCloseOperation( EXIT_ON_CLOSE );
12
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 button!"); JButton button = new JButton("I'm a Swing button!"); //Create a label //Create a label JLabel label = new JLabel(“Number of button clicks: “ + "0 "); JLabel label = new JLabel(“Number of button clicks: “ + "0 "); //Set the label text //Set the label text int numClicks = 0; int numClicks = 0; label.setText(“Number of button clicks: “ + numClicks); label.setText(“Number of button clicks: “ + numClicks);
13
3. Adding components to containers 3. Adding components to containers JPanel pane = new JPanel(); pane.setLayout(new GridLayout(0, 1)); pane.add(button); pane.add(label); frame.getContentPane().add(pane, BorderLayout.CENTER); JPanel pane = new JPanel(); pane.setLayout(new GridLayout(0, 1)); pane.add(button); pane.add(label); frame.getContentPane().add(pane, BorderLayout.CENTER);
14
Recap: GUI part of the Example import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingApplication extends JFrame{ int numClicks = 0; JPanel pane = new JPanel(); JLabel label = new JLabel(“Number of button clicks: “ + numClicks); JButton button = new JButton("I'm a Swing button!"); //Constructor SwingApplication () { super (“SwingApplication”); //set the frame title pane.setLayout(new GridLayout(0, 1)); pane.add(button);pane.add(label); this.getContentPane().add(pane, BorderLayout.CENTER); } this.getContentPane().add(pane, BorderLayout.CENTER); } public static void main(String[] args) { SwingApplication app = new SwingApplication(); app.pack(); app.setVisible(true); } } }
15
4. Handling Events a. 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, WindowListner, etc.) Listener interface(ActionListener, WindowListner, etc.) Object (button, frame, textfield, etc.) who is “listening to” the event Object (button, frame, textfield, etc.) who is “listening to” the event
16
4. Handling Events b. 3 Steps to Implement an Event Handler 1. Implement a listener interface: public class MyClass implements ActionListener public class MyClass implements ActionListener 2. Add the listener to an object: button.addActionListener(this) button.addActionListener(this) 3. Define the methods of the listener interface: public void actionPerformed(ActionEvent e){...//code that reacts to the action..} public void actionPerformed(ActionEvent e){...//code that reacts to the action..}
17
4. Handling Events c. SwingApplication Example Event: ActionEvent Event: ActionEvent Object: button Object: button Interface: ActionListener with the method actionPerformed Interface: ActionListener with the method actionPerformed
18
4. Handling Events c. SwingApplication Example 1. Implement a listener interface: –public class SwingApplication extends JFrame implements ActionListener 2. Add the listener to the button (after it is created) –button.addActionListener(this); –button.addActionListener(this); 3. Define the methods of the listener interface: –Public void actionPerformed (ActionEvent e){ numClicks ++; label.setText (“Number of button clicks: “ + numClicks); }
19
After 2 clicks ….
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.