Week 12
Week 12
Lecture 1
#
Lecture https://youtu.be/A_BV43krS2I
URL
Notion https://21f1003586.notion.site/Graphical-Interfaces-and-Event-Driven-
URL Programming-5b1c1b8d4bc949fbb624221b795744ab
Week # 12
Run time support for language maps low level events to high level events
OS reports low level events: mouse clicked at (x, y), key 'a' pressed
Program sees high level events: Button was clicked , box was ticked ...
Language “sorts” out events and automatically calls the correct function in the
listener
Example
interface ButtonListener {
public abstract void buttonpush(...);
}
Information about the button push even is passed as an object to the listener
Timer
Recall Timer Example
Summary
Event driven programming is a natural way of dealing with the graphical user
interface interactions
Lecture # 2
Lecture
https://youtu.be/sV4ItidrL8s
URL
Notion https://21f1003586.notion.site/Swing-ToolKit-
URL 3dec12f4f8c140c9b3a9ffadd0ecd5e0
Week # 12
Swing ToolKit 1
Exit browser report to all windows currently open
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Swing ToolKit 2
public class ButtonPanel extends JPanel implements ActionListener {
...
}
Create the button, make the panel a listener and add the button to the panel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Listener sets the panel background to red when the button is clicked
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Swing ToolKit 3
...
}
Swing ToolKit 4
// ButtonFrame listens to itself
addWindowListener(this);
main function
Create a JFrame and make it visible
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Ensures that GUI processing does not interfere with other computation
Summary
The swing toolkit has different types of objects
Individual objects have to be embedded in panels which are then added to the
frame
Swing ToolKit 5
☕
More Swing Examples
Type 📒 Lecture
Date @March 22, 2022
Lecture
3
#
Lecture
https://youtu.be/c6Z8BNSv9zY
URL
Notion https://21f1003586.notion.site/More-Swing-Examples-
URL a061c1aa699748b1b9c3adbc2c8e6526
Week # 12
add(yellowButton);
add(blueButton);
add(redButton);
}
...
}
Keep the existing colour if the source is not one of these three buttons
if(source == yellowButton) {
color = Color.yellow;
} else if(source == blueButton) {
color = Color.blue;
} else if(source == redButton) {
color = Color.red;
}
setBackground(color);
Output
import ...;
public class ButtonPanel extends JPanel implements ActionListener {
// Panel has 3 buttons
private JButton yellowButton, blueButton, redButton;
public ButtonPanel() {
yellowButton = new JButton("Yellow");
blueButton = new JButton("Blue");
redButton = new JButton("Red");
...
add(yellowButton);
add(blueButton);
add(redButton);
}
...
}
However, each panel has references only for its local buttons
import ...;
public class ButtonPanel extends JPanel implements ActionListener {
// Panel has 3 buttons
private JButton yellowButton, blueButton, redButton;
public ButtonPanel() {
yellowButton = new JButton("Yellow");
blueButton = new JButton("Blue");
redButton = new JButton("Red");
yellowButton.setActionCommand("YELLOW");
blueButton.setActionCommand("BLUE");
redButton.setActionCommand("RED");
add(yellowButton);
add(blueButton);
add(redButton);
}
...
}
if(cmd.equals("YELLOW")) {
color = Color.yellow;
} else if(cmd.equals("BLUE")) {
color = Color.blue;
} else if(cmd.equals("RED")) {
color = Color.red;
}
setBackground(color);
repaint();
}
...
}
public ButtonFrame() {
...
b1 = new ButtonPanel();
b2 = new ButtonPanel();
contentPane = this.getContentPane();
// Set layout to separate out panels in frame
contentPane.setLayout(new BorderLayout());
contentPane.add(b1, "North");
contentPane.add(b2, "South");
}
}
import ...
public class CheckBoxPanel extends JPanel implements ActionListener {
private JCheckBox redBox;
private JCheckBox blueBox;
public CheckBoxPanel() {
redBox = new JCheckBox("Red");
redBox = new JCheckBox("Blue");
redBox.addActionListener(this);
blueBox.addActionListener(this);
redBox.setSelected(false);
blueBox.setSelected(false);
add(redBox);
add(blueBox);
...
}
}
setBackground(color);
repaint();
}
}
Summary
Swing components such as buttons, checkboxes generate high level events
Event is sent as an object — listener can query the event to obtain details
such as event source, action label, ... and react accordingly
Swing objects are the most aesthetically pleasing, but useful to understand how
GUI programming works across other languages