1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
-
Upload
shawn-walker -
Category
Documents
-
view
214 -
download
0
Transcript of 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
![Page 1: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/1.jpg)
1 / 671
COP 3503 FALL 2012SHAYAN JAVED
LECTURE 14
Programming Fundamentals using Java
![Page 2: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/2.jpg)
2 / 67
Graphical-User Interfaces
(GUIs)
![Page 3: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/3.jpg)
3 / 67
So far...
Only created text-based programs
No fancy graphics (buttons! menus! text-fields!)
![Page 4: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/4.jpg)
4 / 67
Graphical-User Interfaces (GUI)
Going to look at how to create GUIs in Java
![Page 5: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/5.jpg)
5 / 67
Graphical-User Interfaces (GUI)
Going to look at how to create GUIs in Java
Use the SWING API (for desktop-programs)
![Page 6: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/6.jpg)
6 / 67
Graphical-User Interfaces (GUI)
Going to look at how to create GUIs in Java
Use the SWING API (for desktop-programs)
Use the AWT API (for Java Applets – on the web)
![Page 7: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/7.jpg)
7 / 67
The SWING API
Used to create desktop applications.
![Page 8: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/8.jpg)
8 / 67
The SWING API
Used to create desktop applications.
Uses the Model-View-Controller software engineering design pattern.
![Page 9: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/9.jpg)
9 / 67
Model-View-Controller design
Model: Manages the behavior and data of the application. Changes state.
![Page 10: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/10.jpg)
10 / 67
Model-View-Controller design
Model: Manages the behavior and data of the application. Changes state.
View: Renders the model into a form for interaction.
(Button, textbox, checkbox, etc.)
![Page 11: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/11.jpg)
11 / 67
Model-View-Controller design
Model: Manages the behavior and data of the application. Changes state.
View: Renders the model into a form for interaction.
(Button, textbox, checkbox, etc.) Controller:
Receives user input and initiates a response by interacting with the model.
![Page 12: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/12.jpg)
12 / 67
The SWING API
Example:
Scrabble
![Page 13: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/13.jpg)
13 / 67
The GUI API
Use the NetBeans IDE for easy drag-and-drop creation.
![Page 14: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/14.jpg)
14 / 67
The GUI API
Use the NetBeans IDE for easy drag-and-drop creation.
But we are going to focus on basic code
![Page 15: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/15.jpg)
15 / 67
The GUI API
3 Groups of classes:
Component Classes: Buttons, Labels, TextFields, etc.
![Page 16: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/16.jpg)
16 / 67
The GUI API
3 Groups of classes:
Component Classes: Buttons, Labels, TextFields, etc.
Container Classes: Frames, Panels, Applets, etc.
![Page 17: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/17.jpg)
17 / 67
The GUI API
3 Groups of classes:
Component Classes: Buttons, Labels, TextFields, etc.
Container Classes: Frames, Panels, Applets, etc.
Helper Classes: Graphics, Color, Font, etc.
![Page 18: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/18.jpg)
18 / 67
The SWING API
Simple Window
Represented by the JFrame class
![Page 19: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/19.jpg)
19 / 67
The SWING API
import javax.swing.JFrame;
public static void main(String[] args) {
JFrame frame = new JFrame(“A Title”);
frame.setSize(400, 300);
frame.setLocation(10, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
![Page 20: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/20.jpg)
20 / 67
The SWING API – add components
import javax.swing.*;
public static void main(String[] args) {
JFrame frame = new JFrame(“A Title”);
JButton button = new JButton(“OK”);
frame.add(button);
frame.setSize(400, 300);
...
}
![Page 21: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/21.jpg)
21 / 67
The SWING API – add components
A JFrame containts a content pane.
![Page 22: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/22.jpg)
22 / 67
The SWING API – add components
A JFrame containts a content pane.
Content pane = instance of java.awt.Container;
![Page 23: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/23.jpg)
23 / 67
The SWING API – add components
A JFrame containts a content pane.
Content pane = instance of java.awt.Container;
Objects are added to it frame.add( Component )
![Page 24: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/24.jpg)
24 / 67
The SWING API – add components
A JFrame containts a content pane.
Content pane = instance of java.awt.Container;
Objects are added to it frame.add( Component ) frame.remove( Component )
![Page 25: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/25.jpg)
25 / 67
Layout Managers
Components in content pane are laid out by layout managers.
![Page 26: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/26.jpg)
26 / 67
Layout Managers
Components in content pane are laid out by layout managers.
Multiple types: FlowLayout GridLayout BorderLayout
![Page 27: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/27.jpg)
27 / 67
Layout Managers - FlowLayout
Components arranged left to right in order.
One row fills up, a new row is started
![Page 28: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/28.jpg)
28 / 67
Layout Managers - FlowLayout
java.awt.FlowLayout
Properties: alignment: int (CENTER/LEFT/RIGHT/etc.) hgap, vgap: int (the gaps – default: 5 pixels)
![Page 29: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/29.jpg)
29 / 67
Layout Managers - FlowLayout
java.awt.FlowLayout
Properties: alignment: int (CENTER/LEFT/RIGHT/etc.) hgap, vgap: int (the gaps – default: 5 pixels)
Constructors: FlowLayout() FlowLayout(alignment, hgap, vgap)
![Page 30: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/30.jpg)
30 / 67
Layout Managers - FlowLayout
public class ShowFlowLayout extends JFrame {
public ShowFlowLayout() {
// set the flow layout
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20);
add(new JButton(“Button”));
add(new JTextField(8));
}
}
![Page 31: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/31.jpg)
31 / 67
Layout Managers - GridLayout
Arrange components in a grid (matrix) formation. Placed left-to-right.
![Page 32: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/32.jpg)
32 / 67
Layout Managers - GridLayout
Arrange components in a grid (matrix) formation. Placed left-to-right.
Properties: rows, columns: int hgap, vgap: int (the gaps – default: 5 pixels)
![Page 33: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/33.jpg)
33 / 67
Layout Managers - GridLayout
public class ShowGridLayout extends JFrame {
public ShowGridLayout() {
// set the Grid layout – 3 rows, 2 columns
setLayout(new GridLayout(3, 2, 10, 10);
add(new JButton(“Button”));
add(new JTextField(8));
}
}
![Page 34: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/34.jpg)
34 / 67
Layout Managers - BorderLayout
Default Layout for ContentPanes (Jframe)
Divides container into 5 areas: East, West, South, North, Center Components are added into one of these areas
Properties: hgap, vgap: int (the gaps – default: 5 pixels)
![Page 35: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/35.jpg)
35 / 67
Layout Managers - BorderLayout
public class ShowBorderLayout extends JFrame {
public ShowBorderLayout() {
// set the Border Layout
setLayout(new BorderLayout(10, 10);
add(new JButton(“Button”), BorderLayout.EAST);
add(new JTextField(8), BorderLayout.WEST);
}
}
![Page 36: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/36.jpg)
36 / 67
The SWING API
Looked at adding Components to the Window (Frame).
And how to lay them out.
![Page 37: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/37.jpg)
37 / 67
The SWING API
Looked at adding Components to the Window (Frame).
And how to lay them out.
But often need “sub-windows” to create more complex interfaces.
![Page 38: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/38.jpg)
38 / 67
Using JPanels as Subcontainers
Panels are subcontainers.
![Page 39: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/39.jpg)
39 / 67
Using JPanels as Subcontainers
Panels are subcontainers.
Can add components to them
![Page 40: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/40.jpg)
40 / 67
Using JPanels as Subcontainers
Panels are subcontainers.
Can add components to them
Also set layouts (default: FlowLayout)
![Page 41: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/41.jpg)
41 / 67
Using JPanels as Subcontainers
Panels are subcontainers.
Can add components to them
Also set layouts (default: FlowLayout)
JPanel panel = new JPanel();
panel.add(new JButton(“OK”));
![Page 42: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/42.jpg)
42 / 67
Using JPanels
// set the Border Layout of the JFrame
setLayout(new BorderLayout(10, 10);
// add a Panel to the West and East
JPanel p1 = new JPanel();
add(p1, BorderLayout.WEST);
JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(2, 2, 5, 5));
add(p2, BorderLayout.EAST);
// add components to the east panel
p2.add(new JTextField(8)); p2.add(new JButton(“Button1”));
p2.add(new JTextField(8)); p2.add(new JButton(“Button2”));
// one button to the west panel
p1.add(new JButton(“Button3”));
![Page 43: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/43.jpg)
43 / 67
Adding Components
Need to add components for interaction.
![Page 44: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/44.jpg)
44 / 67
Adding Components
Need to add components for interaction.
Some useful ones: JButton JTextField JCheckBox JComboBox JMenuBar etc...
![Page 45: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/45.jpg)
45 / 67
Model-View-Controller design
Model: (ALREADY IMPLEMENTED) Manages the behavior and data of the application. Changes state.
View: DONE Renders the model into a form for interaction.
(Button, textbox, checkbox, etc.) Controller:
Receives user input and initiates a response by interacting with the model.
![Page 46: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/46.jpg)
46 / 67
Interaction and Events
Need to handle events from various GUI components.
![Page 47: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/47.jpg)
47 / 67
Interaction and Events
Need to handle events from various GUI components.
Button clicks, text field changes, menu selection, etc.
![Page 48: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/48.jpg)
48 / 67
Interaction and Events
Need to handle events from various GUI components.
Button clicks, text field changes, menu selection, etc.
Event-driven programming
![Page 49: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/49.jpg)
49 / 67
Interaction and Events
Components generate different kinds of Events
![Page 50: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/50.jpg)
50 / 67
Interaction and Events
Components generate different kinds of Events
ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent
![Page 51: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/51.jpg)
51 / 67
Interaction and Events
Components generate different kinds of Events
ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent
Example: JButton generates ActionEvent
![Page 52: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/52.jpg)
52 / 67
Interaction and Events
Components generate different kinds of Events
ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent
Example: JButton generates ActionEvent Mouse pressed/moved/dragged: MouseEvent
![Page 53: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/53.jpg)
53 / 67
Interaction and Events
Components generate different kinds of Events
ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent
Example: JButton generates ActionEvent Mouse pressed/moved/dragged: MouseEvent JCheckBox: ItemEvent, ActionEvent
![Page 54: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/54.jpg)
54 / 67
Interaction and Events
Need to “listen” for Events.
Source object fires an event, and an object interested in the event handles it. Latter object called a “listener”
![Page 55: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/55.jpg)
55 / 67
Interaction and Events
For an object to be a listener, it needs to implement an interface.
![Page 56: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/56.jpg)
56 / 67
Interaction and Events
For an object to be a listener, it needs to implement an interface.
Interface should correspond to the Event. ActionListener for ActionEvent MouseListener for MouseEvent KeyListener for KeyEvent
![Page 57: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/57.jpg)
57 / 67
Interaction and Events
For an object to be a listener, it needs to implement an interface.
Interface should correspond to the Event. ActionListener for ActionEvent MouseListener for MouseEvent KeyListener for KeyEvent
object.addXListener(...)
![Page 58: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/58.jpg)
58 / 67
ActionListener interface
class AClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
// do whatever you want
}
}
![Page 59: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/59.jpg)
59 / 67
ActionListener interface
class AClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
// do whatever you want
}
}
JButton button = new JButton(“OK”);
button.addActionListener(new AClass());
![Page 60: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/60.jpg)
60 / 67
ActionListener interface
ActionEvent methods:
![Page 61: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/61.jpg)
61 / 67
ActionListener interface
ActionEvent methods: Object getSource(): returns the object on which
the Event initially occurred. (in java.util.EventObject)
![Page 62: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/62.jpg)
62 / 67
ActionListener interface
ActionEvent methods: Object getSource(): returns the object on which
the Event initially occurred. (in java.util.EventObject)
String getActionCommand(): returns the command string
(text of the button for ex.)
![Page 63: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/63.jpg)
63 / 67
ItemListener interface
Used for check boxes, toggle buttons, etc.
class AClass implements ItemListener {
public void itemStatechanged(ItemEvent e) {
// do whatever you want
}
}
![Page 64: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/64.jpg)
64 / 67
ItemListener interface
ItemEvent methods:
Object getItem(): Component-specific object.
int getStateChange(): The new state of the associated object.
![Page 65: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/65.jpg)
65 / 67
Interaction and Events
Look at some other useful Events:
MouseEvent KeyEvent WindowEvent
![Page 66: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/66.jpg)
66 / 67
Interaction and Events
Look at some other useful Events:
MouseEvent KeyEvent WindowEvent
Experiment!
![Page 67: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.](https://reader030.fdocuments.us/reader030/viewer/2022020417/56649f325503460f94c4d906/html5/thumbnails/67.jpg)
67 / 67
Summary
Windows and Panels Layouts Add Components
Add Listeners Handle Events