Lecture 8 - Swing continued Flashcards
Why do we need a Model-View-Controller style for programming?
Otherwise, code will become unreadable
How does the use of Model-View-Controller style programming vary by size?
Most simple: MVC in one class
Medium size & complexity: Program may have the a class with the View and Controller & a main class that creates 2 objects (View-Controller object & Model object)
Longest & most complex programs may have the MVC separate. View (constructor to create display, helper methods to update display) Controller (actionPerform & other helper methods for the interaction) Main class creates 3 objects (Model, View, Controller)
What does the event listener KeyListener do?
It detects keys
What imports do we need for KeyListener?
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
What does charAt(0) do?
Returns the char value at the specified index.
An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing
Write a for loop that copies something into an arrray,
for(int i=0;i
What is the class header for using a KeyListener and an ActionListener?
public class ex extends JFrame implements ActionListener{
“b” handles events, but constructs the variables/array in another class “a”.
In “b” you want to get the variable String keyword that you are passing to the constructor and create a new object of class “a”.
How do you do this?
String text = keyword.getText();
a = new a (text);
What is the “model” class?
Holds the data
Private variables, methods to perform calculations, getters & speaks to the controller but doesn’t know the view exists.
What is the “view” class?
The interface.
- Swing stuff (ie., extends, components, panels …)
- Getters (ie. getTextField())
- Setters for the controller to use to set the calculations in the Model class with the input from the View class.
- An actionListener is added to the button so when its clicked the controller is alerted
- Handle errors using a JOptionPane that shows an error message
What is the “controller” class?
It handles interactions between the view and the model in an actionPerformed() method, but doesn’t actually do anything itself.
Implements ActionListener
Think of it as the SERVER in the client-database relationship.
How would you get the input from a text field and return is as an Integer?
use a get()
return Integer.parseInt(.getText())
What is typically in the “view” class?
- extends JFrame so it needs all the information to set up a JFrame
- private global variables ie.,JLabel
- a constructor that has the JFrame methods ie., setSIze etc, adds the variables to the JPanel and then adds them to this JPanel
- getters to access the variables in other classes (JTextField will need to be converted to an int if we are looking for numbers etc)
- an ActionListener method that lets the controller know that the button has been clicked etc
- JOptionPane that shows an error if the input is incorrect
What is typically in the “controller” class?
- A constructor that takes in the Model & the View class
- Handles events by adding actionPerformed method
- Includes a try catch block incase there is an unchecked error eg., someone enters 1 number instead of 2
How do you use a JPanel that you made in another class?
- in extends JFrame implements ActionListener
- create an object with the panel class name and pass the current class “this” into it. This means that everything will be constructed in the JPanel class.