chapter 3© copyright janson industries 20141 ▮ explain difference between: ▮...

90
Chapter 3 © copyright Janson Industries 2014 1 Explain difference between: Applications/Applets/Servlets AWT/Swing/SWT Inheritance Basic AWT visual components UML (Unified Modeling Language) GUI Java Non-graded assg part 1 part2

Upload: marcia-bridges

Post on 28-Dec-2015

235 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 1

▮ Explain difference between:

▮ Applications/Applets/Servlets

▮ AWT/Swing/SWT

▮ Inheritance

▮ Basic AWT visual components

▮ UML (Unified Modeling Language)

GUI Java

Non-graded assg part 1 part2

Page 2: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 2

▮ Apps are stored & executed on the client PC

▮ Applets are “called” by a web page. The applet’s Java code▮ Stored on the server▮ Downloaded to the client PC▮ Run by the client browser

▮ Servlets are stored and run on the server▮ Servlets generate HTML that is sent to the

client’s browser

Apps/Applets/Servlets

Page 3: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 3

Apps/Applets/Servlets

Stored on client

Run on client

Stored on server

Run on server

Apps X X

Applets X X

Servlets X X

Page 4: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 4

Apps/Applets/Servlets

Stored on Run on

Apps client client

Applets server client

Servlets server server

Page 5: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 5

▮ Client based apps Adv:

▮ Fast: execute on client, no communication lag

▮ No catastrophic failure

▮ Client based apps DisAdv:

▮ Must install app on each client computer▮

▮ Multiple copies of app:▮ Take up more space▮ Updating app more difficult/time consuming

Client Apps

Page 6: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 6

▮ Server based apps Adv:

▮ Accessible from any computer with a browser

▮ One copy of app▮ Takes up little space▮ Updates to app are fast

▮ Server based apps DisAdv:

▮ Catastrophic failure – server goes out, no processing can occur

▮ Communication time lag – especially with applets

▮ Must download entire application before executing

Server Apps

Page 7: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 7

▮ Many sets of GUI components:▮ AWT (Advanced Windowing Toolkit)▮ Swing▮ SWT (Simple Widget Toolkit)

▮ AWT implements each H/W platform’s version of the GUI components

▮ Swing is consistent across all H/W (but takes longer to “paint” the GUI)

Java GUI Components

Page 8: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 8

▮ As much as possible, SWT implements each H/W’s platforms version of the GUI components

▮ Provides the added Swing components and functions

▮ Works differently than AWT and Swing

GUI Java

Page 9: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 9

▮ AWT and Swing have similar sets of GUI components stored in packages (that are part of the JDK):▮ Frame - JFrame▮ Button - JButton▮ TextField - JTextField▮ Label - JLabel

▮ To use the GUI components, must “import” the AWT or Swing classes into your class

GUI Java

Page 10: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 10

GUI Java

FRAME JFRAME

Page 11: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 11

Using GUI Components

▮ AWT GUI classes in java.awt package

▮ You can use them by fully specifying their location▮ java.awt.Label empName = new

java.awt.Label();

▮ Or, use the import statement to identify the java.awt package▮ import tells complier to look in that location

for any non-fully specified classes▮ Sorta like the path and classpath but for the

compiler not JVM or Windows

Page 12: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 12

GUI Components

// GUIEx.java

import java.awt.*;

public class GUIEx extends Frame { : : : : : :

Using import allows easier access to the GUI components because you

don’t have to fully specify the location of the GUI component

We define this class as a Frame with the extends “clause” in the class header

This make GUIEx a subclass of Frame

Page 13: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 13

Subclass/Superclass

▮ Subclasses inherit all methods and variables of the superclass (also called Specialization)

▮ You can create superclasses to hold common information and functions

▮ For instance:▮ A Person superclass with name, address,

and phone number variables▮ Subclasses Employee, Customer, and

Supplier would all get these variables

Page 14: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 14

Subclass/Superclass

▮ Subclasses inherit all methods and variables of the superclass (aka Specialization)

PersonString name, addr, phnum;

Supplier extends PersonString SuppNum;name = "Pat";addr="3 3rd St";phnum="333-3333";

Customer extends PersonString CustNum;name = "Mary";addr="2 2nd St";phnum="222-2222";

Employee extends PersonString EmpNum;name = "Joe";addr="1 1st St";phnum="111-1111";

Page 15: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 15

Create a new class (Cust2) – File, New, Class

Click Browse...

Page 16: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 16

Start typing “Frame”, select Frame, (and make sure its from the java.awt package), click OK then Finish

Page 17: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 17

RAD creates a bare minimum class

Page 18: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 18

Labels

▮ Display constant text on a frame

▮ Must define a label object and, optionally, set it’s text property

Label custNameLabel = new Label(“Joe Customer");

Page 19: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 19

But you get an error

Click light bulb icon to get possible solutions

Page 20: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 20

Click a solution to show the solution code

Double click solution to insert code

RAD will list solutions with most likely at top

Page 21: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 21

What happens if you click the Run button and why?

Page 22: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 22

When running, RAD first suggests saving changes

It’s usually a good idea: click OK

Page 23: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 23

Gotcha: if you click the Run button, RAD will run Cust1

(the last application that was run)

Page 24: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 24

Can display the Run drop down menu

Notice Cust1 is the first listed Run Configuration

This means it is the default application to be run

Can click Run Configurations...

Page 25: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 25

Specify Cust2

Click Run

What’s going to happen and why?

Page 26: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 26

Oops, forgot to create a main method that instantiates (creates) a Cust2 object

Page 27: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 27

Getting Cust2 to work

▮ Since Cust2 will be instantiated you need

▮ A constructor for the class (i.e. must have method called Cust2)

▮ In addition, to get the frame and labels to work you must set their properties:

▮ Define sizes for the frame and labels

▮ Position the labels on the frame

▮ Make the frame visible

Page 28: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 28

Frames

▮ In the constructor, specify:

this.setSize(600, 400);

this.setLayout(null);

(this refers to “this object”, i.e. the Cust2 frame)

▮ Layout null tells the system not to use the predefined BorderLayout▮ You will position the components

individually

Page 29: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 29

Labels

▮ In constructor, set label size & location:

▮ labelVariableName.setSize(length, height)

▮ labelVariableName.setLocation(from left, from top)

▮ Numbers are in pixels

▮ Example (after the label object is created and after this.setLayout(null);) add:

▮ custNameLabel.setSize(100,10);

▮ custNameLabel.setLocation(155,135);

Page 30: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 30

Labels

▮ In the constructor, after the label properties are set, add the label to the frame object (referred to as this)

▮ this.add(custNameLabel);

▮ At the end of the constructor, make the frame visible

▮ this.setVisible(true);

Page 31: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 31

main method

▮ In the main method, must create a Cust2 object

▮ We will assign it to a variable called custTest

▮ Cust2 custTest = new Cust2();▮ (Actually, don't need to create variable and assign object

because we will not reference the Cust2 object)

▮ Time to run!

Page 32: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 32

Display Run menu – notice Cust2 is default launch

Click Cust2 to run

Page 33: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 33

Results in Cust2 object being created (instantiated)

Page 34: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 34

▮ 1. Create a class as a subclass of Frame

▮ 2. Set the following Frame properties:▮ Layout to null▮ Size▮ Location (optional)▮ Visible to true

▮ 3. To view the frame, create an instance of the Frame subclass

Steps to Create and Display a Working Frame

Page 35: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 35

How to Create a Working Label

▮ 1. Import the Label class

▮ 2. Create a Label object

▮ 3. Set the following label properties:▮ Size▮ Location▮ Text (optional)

▮ 4. Add the label to the frame

Page 36: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 36

Cust2 (or any Frame subclass) does not inherit a close function from

Frame

To close the frame, click the Terminate button in the Console pane tool bar

Page 37: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

import java.awt.Frame;

import java.awt.Label;

public class Cust2 extends Frame {

Label custNameLabel = new Label("Joe Customer");

Label custAddressLabel = new Label("123 Main St.");

Label custCSZLabel = new Label("Jax, FL 32246");

public Cust2(){

this.setSize(600, 400);

this.setLayout(null);

custNameLabel.setSize(100,10);

custNameLabel.setLocation(155,135);

this.add(custNameLabel);

custAddressLabel.setSize(100,10);

custAddressLabel.setLocation(155,155);

this.add(custAddressLabel);

custCSZLabel.setSize(100,10);

custCSZLabel.setLocation(155,175);

this.add(custCSZLabel);

this.setVisible(true);

}

public static void main(String[] args){

Cust2 custTest = new Cust2();

}

}

Chapter 3 © copyright Janson Industries 2014 37

Non Graded Assg – Part 1

▮ Create Cust2 so it displays the same info as Customer but in a frame with 3 labels

Page 38: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 2 © copyright Janson Industries 2014 38

Instead of displaying static info, we can pass the frame info to display

Like Cust1 did

Page 39: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 39

Passing Info

▮ Need to change the frame's constructor to:

▮ Accept info

▮ Set the label value to the passed info

Page 40: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 40

Passing Info

Constructor must accept

infoConstructor must set label value

Info must be passed

Page 41: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 41

Passing Info

Problem: if 7 pieces of info need to be passed,

How easy is it to remember what’s the order?When is phone # passed: First? Second?

Sixth?Solution: object properties

If 2 pieces of info need to be passed,public Cust3(String name, String address){…

Page 42: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 42

Properties

▮ Already worked with properties ▮ Label text and location, Frame size and layout

▮ In a class, each property is:

▮ Defined as a private class variable

▮ Has a getter method that returns the property value

▮ Has a setter method (usually with validation functions) that sets the value of the property

Page 43: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 43

Properties

▮ Usually you will create an object

▮ Then manipulate the object properties▮ We created a label, then set its size, location,

text

▮ For instance:

▮ We’ll redefine the Customer class to have properties that hold the following info:

▮ Contact Person, Customer Name, Phone Number, Ship to Address

Page 44: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 44

Creating Properties

▮ Define private variables for each property

Page 45: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 45

Let RAD create the getters and setters

Click Source then Generate Getters and Setters...

Page 46: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 46

Properties

▮ Select getters and setters or click Select All

▮ Can specify▮ Where methods should

be in the class▮ How they are grouped

▮ Click OK button

▮ Generates 14 methods

Page 47: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 47

Can't fit all getters/setters in work areaCan see them all in the outline

Page 48: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 48

Using Properties

▮ Just as you set Frame and Label object properties, a Customer object’s properties can now be set

▮ Then pass the Customer variable (assigned to the Customer object) instead of individual variables/values

▮ We will modify the customer application so that a Customer variable is passed to a Customer Frame

Page 49: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 49

Using Properties

▮ A new class CustApp will be used to "kick off the application"

▮ This means CustApp will:

▮ Create a Customer object and assign it to a Customer variable

▮ Set the Customer object's properties

▮ Create a Customer Frame object and pass it the Customer variable (instead of individual variables/values)

Page 50: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 50

New Customer Example

CustomerObject

CustFrameObject

1CustApp creates a Customer object, assigns to variable c, sets object’s properties

2CustApp creates a CustFrame object, assigns to variable

cf, and sends Customer variable

c

3CustFrame retrieves &

displays Customer object properties

CustApp

Page 51: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 51

New Customer Example

CustomerObject

CustFrameObject

1

2CustApp creates a CustFrame object, assigns to variable

cf, and sends Customer variable

c

3

CustApp

Data Data

Page 52: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 52

Using Properties

▮ Create a new Frame subclass CustFrame▮ CustFrame constructor accepts a variable of

type Customer and assigns it to a variable named cust

▮ CustFrame has four labels▮ custName▮ shipToLbl1▮ shipToLbl2▮ contactInfo

▮ CustFrame retrieves info from the Customer object and puts the info into the labels

Page 53: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 53

Select project (JavaCourse) then File, New, Class

Specify name of Class and superclass

Page 54: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 54

Need to define a constructor to accept a Customer

variable

Initial code

Cust Frame

import java.awt.Frame;

public class CustFrame extends Frame {

}

Page 55: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 55

Programming Technique

▮ We will code portions of the class, then test each portion as it is created

▮ Incremental coding makes finding errors easier

▮ If a new method is added and RAD shows an error, it has something to do with the new code

▮ If all the methods coded at once results in 42 errors, harder to determine what the errors are

Page 56: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 56

Don’t forget, for frame to work need to:

Size the frame : this.setSize(300, 282); Set layout to null: this.setLayout(null);

Set visible: this.setVisible(true);

import java.awt.Frame;

public class CustFrame extends Frame {

public CustFrame(Customer cust) {

}

}

Cust Frame

Even though CustApp passes a variable called c, CustFrame stores it in a variable

named cust

Page 57: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 57

▮ Import the Label class: import java.awt.Label;

▮ Create 4 label objects and assign to variables

▮ Size and position the labels

Label custNameLbl = new Label();

Label shipToLbl1 = new Label();

Label shipToLbl2 = new Label();

Label contactInfo = new Label();

Cust Frame Labels

custNameLbl.setBounds(62, 65, 176, 23);

shipToLbl1.setBounds(62, 120, 176, 23);

shipToLbl2.setBounds(62, 175, 176, 23);

contactInfo.setBounds(62, 230, 176, 23);

Page 58: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 58

▮ Set some test text

▮ Add the labels to the frame

▮ Test it : what do we need?

this.add(custNameLbl);

this.add(shipToLbl1);

this.add(shipToLbl2);

this.add(contactInfo);

Cust Frame Labels

custNameLbl.setText("test text");

shipToLbl1.setText("test text");

shipToLbl2.setText("test text");

contactInfo.setText("test text");

Page 59: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

public static void main(String[] args) {

Customer c = new Customer();

CustFrame cf = new CustFrame(c);

}

▮ Code a main method in CustFrame to create a CustFrame object (to test so far)

Chapter 3 © copyright Janson Industries 2014 59

CustFrame Labels

▮ In class assg: Try it!

Page 60: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 60

▮ Need code to retrieve data from Customer object and put into labels

▮ Use Customer object getter to retrieve a property value

▮ Could do like this

Cust Frame Labels

String tempName = cust.getCustName();

custNameLbl.setText(tempName);

String tempStreet = cust.getShipToStreet();

shipToLbl1.setText(tempStreet);

Page 61: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 61

▮ More efficient to do like this

▮ Two less statements no temp variables

▮ These labels are easy: one getter retrieves one label’s text

▮ Need to use concatenation for other label’s text▮ I.e. multiple getter return values go into these

labels

Cust Frame Labels

custNameLbl.setText(cust.getCustName());

shipToLbl1.setText(cust.getShipToStreet());

Page 62: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 62

Concatenation

▮ Combines 2 strings into one▮ String a = new String(“Sam”);▮ String b = new String(“I am”);▮ a + b would be equal to “SamI am”

▮ Use concatenation (+) to combine multiple property values into one label shipToLbl2.setText(cust.getShipToCity() + ", " +

cust.getShipToState() + " " + cust.getShipToZip());

contactInfo.setText(cust.getContactPerson() + " Ph: " + cust.getContactPhone());

Page 63: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 63

Customer

▮ So we’ve:▮ Added properties to Customer▮ Created CustFrame to display the properties

▮ Need to create a CustApp that▮ Creates a Customer object, a Customer variable

named c, and assigns the object to c▮ Sets values for the Customer properties▮ Creates a CustFrame object and passes the

Customer variable c, creates a CustFrame variable named cf, assigns the CustFrame object to cf

Page 64: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 64

CustApp

▮ Create a class called CustApp▮ In CustApp

▮ Create a main method

▮ In main, write code to:▮ Create a Customer object, assign to variable c▮ Use setters to assign the following values in

the Customer object associated with variable c:▮ Kindness Foods, 1 Milkof St., Human, ME 03234▮ Joe Samaritan, 555-3333

▮ Create a CustFrame object and pass c, assign the CustFrame object to a variable named cf

Page 65: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 65

Have RAD generate the main method

Page 66: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

import java.awt.Frame;

import java.awt.Label;import java.awt.Frame;import java.awt.Label;

public class CustFrame extends Frame {Label custNameLbl = new Label();Label shipToLbl1 = new Label();Label shipToLbl2 = new Label();Label contactInfo = new Label();

public CustFrame(Customer cust) { this.setSize(300, 282);this.setLayout(null); this.setVisible(true);custNameLbl.setBounds(62, 65, 176, 23);shipToLbl1.setBounds(62, 120, 176, 23);shipToLbl2.setBounds(62, 175, 176, 23);contactInfo.setBounds(62, 230, 176, 23);custNameLbl.setText(cust.getCustName());shipToLbl1.setText(cust.getShipToStreet());shipToLbl2.setText(cust.getShipToCity() + ", " + cust.getShipToState() + " " + cust.getShipToZip());contactInfo.setText(cust.getContactPerson() + " Ph: " + cust.getContactPhone());this.add(custNameLbl);this.add(shipToLbl1);this.add(shipToLbl2);this.add(contactInfo);}

public static void main(String[] args) { Customer c = new Customer(); CustFrame cf = new CustFrame(c); }

}

public class CustApp {

public static void main(String[] args) {

Customer c = new Customer();

c.setContactPerson("Joe Samaritan");

c.setContactPhone("555-3333");

c.setCustName("Kindness Foods");

c.setShipToStreet("1 Milkof St.");

c.setShipToCity("Human");

c.setShipToState("ME");

c.setShipToZip("03234");

CustFrame cf = new CustFrame(c);

}

}

Chapter 3 © copyright Janson Industries 2014 66

Non-graded assg: Finish creating the Customer Application

When run should look like

Page 67: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 67

Non-graded Assg

▮ Export as one jar file▮ Cust2.java

▮ Customer.java

▮ CustFrame.java

▮ CustApp.java

▮ Email the jar file as an email attachment to [email protected]

Page 68: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 68

UML

▮ Unified Modeling Language

▮ Diagrams to show class methods and variables and interclass relationships:

▮ Composition

▮ Specialization

Page 69: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 69

Class Diagram Example

▮ For each class a box is created

▮ The box consists of three areas:

▮ Identification (name)

▮ Attributes (variables)

▮ Operations (methods)

▮ Attributes and Operations are further identified as public or private (+,-)

Customer

- name:String- address:String

+ setName:void+ setAddress:void

Page 70: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 70

Class Diagram Example

▮ Attribute entries also specify the attribute’s type (the type is preceded by a colon & follows the attribute name)

Customer

-name:String-address:String

+setName:void+setAddress:void

▮ Operation entries also specify a return value (the return value is preceded by a colon & follows the operation name)

Page 71: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 71

Class Diagram Example

▮ Attribute entries can also specify an initial value:

Attribute:Type=initial_value

name:String=“Joe Programmer”

▮ Operation entries can also specify the expected parameter(s) type(s):

Operation(parm_list):returned_value

getCustomerPO(int):String

Page 72: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 72

UML Relationship Diagrams

▮ Can show specialization

Object

Applet

Component

TextComponentContainerButton

Panel TextField

Frame

Window

Page 73: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

© copyright Janson Industries 2014 Chapter 3 73

RAD can generate for you

Right click a java file (CustApp)

Scroll to bottom and choose:

Visualize

Add to New Diagram File

Class Diagram

Page 74: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 74

Click Finish

Page 75: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 75

Initially shows class diagram for that class

As you add, will show composition relationship

Right click Customer and CustFrame

Choose

Visualize

Add to current diagram

Page 76: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 76

Can't fit diagram in window

In outline view, click Overview button to display “the big picture”

Can drag boxes, close Palette, & resize window to fit

Page 77: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 77

Initially only shows composition (use relationship)

Page 78: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 78

Need to simplify diagram

Select all three boxes (Ctrl+click)

Right click one box and choose:

Filters

Show/Hide Compartment

Name Compartment only

Page 79: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 79

Want to see superclasses, must add to diagram

Expand JRE System Library, vm.jar, java.lang

Page 80: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 80

Select Object.class and String.class

Right click either class and select Visualize then Add to Current Diagram

Page 81: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 81

Close up vm.jar and expand rt.jar and java.awt

Page 82: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 82

Scroll down and select Frame.class and Label.class

Right click either class and select Visualize then Add to Current Diagram

Page 83: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 83

Right click anywhere on diagram background and choose Select then All Shapes

Page 84: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 84

Right click Frame box and select Filters, Show/Hide Compartment, Name Compartment Only

Page 85: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 85

Right click background and choose Arrange All

Page 86: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 86

Still need to drag boxes into better arrangement and resize the boxes

Page 87: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 87

You can modify which relationships are shown

Page 88: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 88

You can modify which relationships are shown

Page 89: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 89

New diagram file in the project

Page 90: Chapter 3© copyright Janson Industries 20141 ▮ Explain difference between: ▮ Applications/Applets/Servlets ▮ AWT/Swing/SWT ▮ Inheritance ▮ Basic AWT visual

Chapter 3 © copyright Janson Industries 2014 90

Other Resources

▮ JavaRanch: http://www.javaranch.com/▮ Forums, faqs, tutorials

▮ http://www.javaranch.com/campfire has stories/tutorials concerning various java topics

▮ Here is a link to some online java videos:▮ http://www.youtube.com/user/webhasher/

videos?sort=da&view=u

▮ I looked at several of them and they seemed pretty good