Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4© copyright Janson Industries 20141 More GUI ▮ Events, Actions, and Listeners ▮ The Visual Editor ▮ More GUI component properties Non-graded assg.

Similar presentations


Presentation on theme: "Chapter 4© copyright Janson Industries 20141 More GUI ▮ Events, Actions, and Listeners ▮ The Visual Editor ▮ More GUI component properties Non-graded assg."— Presentation transcript:

1 Chapter 4© copyright Janson Industries 20141 More GUI ▮ Events, Actions, and Listeners ▮ The Visual Editor ▮ More GUI component properties Non-graded assg part1 part2part1part2

2 Chapter 4© copyright Janson Industries 20142 WindowListener ▮ A class that reacts to window events ▮ Window events ▮ Clicking X to close the window ▮ Resizing the window ▮ This is another one of those JDK classes that makes your programming life easier!

3 Chapter 4© copyright Janson Industries 20143 WindowListener ▮ For a class to be a WindowListener it must: import java.awt.event.WindowEvent; import java.awt.event.WindowListener; ▮ In the class header have: … implements WindowListener {

4 Chapter 4© copyright Janson Industries 20144 WindowListener ▮ Also, the WindowListener must have these seven methods : public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} Ends the JVM

5 Chapter 4© copyright Janson Industries 20145 WindowListener ▮ Two ways you can do this: ▮ 1. Create a separate class to act as the window listener ▮ Then in the frame class: ▮ Create the window listener object ▮ Add the window listener object to the frame this.addWindowListener(new WindowListener()); ▮ 2. Have the frame implement WindowListener ▮ Add the frame to itself this.addWindowListener(this);

6 Chapter 4© copyright Janson Industries 20146 WindowListener ▮ extends relates two classes as super/sub classes ▮ implements defines the class as a WindowListener (in addition to whatever the extends specifies, e.g. Frame) ▮ addWindowListener makes the WindowListener part of the frame ▮ Just like creating & adding a label to the frame

7 Chapter 4© copyright Janson Industries 20147 WindowListener Create a separate class to act as the window listener Must import WindowEvent and implement Window Listener

8 Chapter 4© copyright Janson Industries 20148 WindowListener Create the window listener object Add the window listener object to the frame

9 Chapter 4© copyright Janson Industries 20149 Add the frame to itself Option 2: Define the frame as a WindowListener Have the frame import and implement WindowListener Either way, close button will now work

10 Chapter 4© copyright Janson Industries 201410 Superclass/subclass ▮ Problem with option 2 ▮ Every frame must have code ▮ Alternative 3 rd solution ▮ Use inheritance/specialization to avoid coding these methods multiple times ▮ Have a superclass with needed code ▮ Advantages: ▮ Save programmer time ▮ Less chance of error ▮ Easier to modify ▮ P.S. Inherited methods and variables can be overridden in subclass (more on this later)

11 Chapter 4© copyright Janson Industries 201411 Visual Editor ▮ Create a new CustomerFrame class that implements WindowListener ▮ CustomerFrame constructor will expect a Customer variable ▮ CustomerFrame retrieves info from Customer object and puts into labels ▮ Will create a Visual Class and use the Visual Editor

12 Chapter 4© copyright Janson Industries 201412 New Customer Example CustApp Customer Object CustomerFrame Object Create a Customer object, assign to variable c, set object’s properties Create a CustomerFrame object, pass c, assign to variable cf Retrieve & display Customer object properties

13 Chapter 4© copyright Janson Industries 201413 Visual Class ▮ Click File, New, Visual Class

14 Chapter 4© copyright Janson Industries 201414 Specify CustomerFrameUse the Style box to specify Frame as superclass Have RAD generate main and window event handling methods Implement by clicking Add.. 1 2 3 4

15 Chapter 4© copyright Janson Industries 201415 Start typing WindowListener, select WindowListener from the AWT package, click OK

16 Chapter 4© copyright Janson Industries 201416 Will use main method for testing. Click Finish.

17 Chapter 4© copyright Janson Industries 201417 import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class CustomerFrame extends Frame implements WindowListener { private static final long serialVersionUID = 1L; public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public static void main(String[] args) {} public CustomerFrame() { super(); initialize(); } private void initialize() { this.setSize(300, 200); this.setTitle("Frame"); } Initialize method used to set frame properties and add components VE initially generates this code

18 Chapter 4© copyright Janson Industries 201418 Design pane Source pane Resize Open Component Palette

19 Chapter 4© copyright Janson Industries 201419 Visual Editor ▮ Components stored in trays ▮ Click on tray to open/close ▮ Click on component to select ▮ Then click on design pane to add selected component ▮ Click on source pane to edit source code

20 Chapter 4© copyright Janson Industries 201420 Change all properties in Properties pane Enter/change source code Open/close trays Select a component Add components and change some properties in design pane

21 ▮ Make frame visible and layout null by adding this source code into initialize ▮ Or change in Properties view Chapter 4© copyright Janson Industries 201421 Visual Editor this.setLayout(null); this.setVisible(true);

22 Chapter 4© copyright Janson Industries 201422 Click Label in palette then click on design pane to add Should give the Label variable a meaningful name

23 Chapter 4© copyright Janson Industries 201423 Or accept the default Label's source code and property values are displayed

24 Chapter 4© copyright Janson Industries 201424 Visual Editor ▮ Modify component in design pane, properties pane, or source code ▮ VE does not name components well (i.e. label, label1, label2, etc.) ▮ Right click component and select rename field

25 Chapter 4© copyright Janson Industries 201425 Specify new name, click OK, and save all modified resources VE changes all the source code

26 Chapter 4© copyright Janson Industries 201426

27 Chapter 4© copyright Janson Industries 201427 Labels ▮ Lots of label properties ▮ Foreground and background colors ▮ Size, Location ▮ Font style ▮ Alignment ▮ Some nice VE features to define labels

28 Chapter 4© copyright Janson Industries 201428 Labels ▮ Select many components by ▮ Holding CTRL key and clicking each OR ▮ Change pointer from selection to marquee then click and drag around all the components

29 Chapter 4© copyright Janson Industries 201429 Click Marquee and pointer changes from arrow to +...to here, so all components are inside the dotted line box Click here and drag... I’ve added 3 more labels

30 Chapter 4© copyright Janson Industries 201430 When released all components selected, any changes will change all Alignment and size changes according to the anchor component Alignment button brings up

31 Chapter 4© copyright Janson Industries 201431 Anchor ▮ When selecting by Marquee, it is the newest component ▮ When selecting by Selection, it is the last component selected ▮ Anchor component indicated with solid resize handles

32 Chapter 4© copyright Janson Industries 201432 Changed pointer to Selection, then reselected so oldest label is anchor (held CTRL key, clicked each label) Click left align

33 Chapter 4© copyright Janson Industries 201433 Resize all by dragging any label resize handle

34 Chapter 4© copyright Janson Industries 201434 Need to align labels vertically Click distribution box then distribute vertically

35 Chapter 4© copyright Janson Industries 201435 Distributes evenly within the box. Make distribution box larger, distribute vertically again

36 Chapter 4© copyright Janson Industries 201436 Drag labels to center on frame Click distribution box button again (to deselect), then close Customize Layout window

37 Chapter 4© copyright Janson Industries 201437 Visual Editor ▮ Rename the labels from top to bottom ▮ custNameLbl, shipToLbl1, shipToLbl2, contactInfo ▮ Make sure custNameLbl is on top ▮ Add code to: ▮ receive a Customer variable ▮ move the info from the Customer object into the labels

38 Chapter 4© copyright Janson Industries 201438 Visual Editor ▮ Need to create a class variable of type Customer ▮ private Customer customer = null; ▮ Change constructor to accept a Customer variable ▮ public CustomerFrame(Customer cust) { ▮ In constructor, move method variable (cust) to class variable (customer) – why? ▮ customer = cust;

39 Chapter 4© copyright Janson Industries 201439 Change CustApp so that a CustomerFrame object is created and change Customer Frame so that info is displayed like this Enable the Window Close button Non-graded assg:

40 Chapter 4© copyright Janson Industries 201440 Visual Editor Probs ▮ “Error trying to set new file into editor” ▮ 1) Bring up a VE session and close it. Make sure no VE's are open. 2) Click Project, then Clean..., then click Clean All Projects radio button, then OK button 3) Wait for build to complete. 4) Try VE again. It should work.

41 Chapter 4© copyright Janson Industries 201041 Visual Editor Probs ▮ When creating, Visual Class not an option ▮ When RAD is installed the Visual Editor is not in the default configuration ▮ You must change the configuration to include the Visual Editor ▮ Go back to the Ch01a slides ▮ Slides 37 – 46 show how to change the configuration to include VE

42 Chapter 4© copyright Janson Industries 201442 ActionListener ▮ “Reacts” to actions on buttons and lists ▮ Actions ▮ Clicking a button ▮ Clicking a list item ▮ Need to add: import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

43 Chapter 4© copyright Janson Industries 201443 ActionListener ▮ In the class header, define class as an ActionListener by adding: …extends Frame implements ActionListener { ▮ Add the ActionListener to a component: ▮ myButton.addActionListener(this); ▮ In the ActionListener class must define an actionPerformed method ▮ This method is executed when an action occurs

44 Chapter 4© copyright Janson Industries 201444 CustomerFrame1 ▮ Create a visual class CustomerFrame1 ▮ Implement both action and window listeners ▮ Has a button that when pressed will display the data

45 Chapter 4© copyright Janson Industries 201445 CustomerFrame1 Initially displayed with no data Click Display button…

46 Chapter 4© copyright Janson Industries 201446 CustomerFrame1 … info displayed in labels

47 Chapter 4© copyright Janson Industries 201447

48 Chapter 4© copyright Janson Industries 201448 import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class CustomerFrame1 extends Frame implements WindowListener, ActionListener { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent e) {} public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} : ::: public void windowOpened(WindowEvent e) {} public static void main(String[] args) {} public CustomerFrame1() { super(); initialize(); } private void initialize() { this.setSize(300, 200); this.setTitle("Frame"); } RAD took care of imports and class header Took care of actionPerformed and window methods Initialize sets Frame props and (as added) component properties

49 Chapter 4© copyright Janson Industries 201449 CustomerFrame1 ▮ Notice that the “window” methods don’t do anything ▮ Need to add code to remove window this.dispose(); ▮ “Code a little, test a little” ▮ Verify close button works by running app ▮ In main, instantiate a CustomerFrame1 object ▮ Then run CustomerFrame1 as application

50 Chapter 4© copyright Janson Industries 201450 CustomerFrame1 ▮ Nothing happens – why not? ▮ Never set Frame visible ▮ Wish I had created a useful frame superclass that automatically did that! ▮ Add code to set visible then ▮ Run CustomerFrame1 as application ▮ Test close button

51 Chapter 4© copyright Janson Industries 201451 Click Frame to select and display properties In properties pane, click visible’s drop down button and select true

52 Chapter 4© copyright Janson Industries 201452 import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class CustomerFrame1 extends Frame implements ActionListener, WindowListener{ public void actionPerformed(ActionEvent arg0) {} public void windowOpened(WindowEvent arg0) {} public void windowClosing(WindowEvent arg0) { this.dispose(); } : : : : : public void windowDeactivated(WindowEvent arg0) {} public static void main(String[] args) { CustomerFrame1 cf1 = new CustomerFrame1(); } public CustomerFrame1() { super(); initialize();} private void initialize() { this.setSize(300, 200); this.setTitle("Frame"); this.setVisible(true); } VE added to source You need to add to source

53 Chapter 4© copyright Janson Industries 201453 CustomerFrame1 ▮ If run and close button clicked, nothing happens – why not? ▮ Never added WindowListener to the frame ▮ This is why you should test often: catch errors early!! ▮ Add code in initialize() to add WindowListener and test this.addWindowListener(this);

54 Chapter 4© copyright Janson Industries 201454 CustomerFrame1 ▮ Change source code so that ▮ Class Customer variable called customer is created ▮ Constructor: ▮ accepts a Customer variable called cust ▮ assigns cust to the class variable customer ▮ Why? ▮ So actionPerformed() can access the Customer object

55 Chapter 4© copyright Janson Industries 2014 55 public class CustomerFrame1 extends Frame implements ActionListener, WindowListener { private Customer customer = null; : : : : public CustomerFrame1(Customer cust) { super(); customer = cust; initialize(); } : : : : } CustomerFrame1 Added

56 Chapter 4© copyright Janson Industries 201456 Get an error: why? How fix?

57 Chapter 4© copyright Janson Industries 201457 CustomerFrame1 ▮ Our test in main does not pass the required Customer variable ▮ In main must: ▮ Instantiate a Customer object and assign to a Customer variable ▮ Pass Customer variable to CustomerFrame1

58 Chapter 4© copyright Janson Industries 201458 public static void main(String[] args) { Customer cust = new Customer(); cust.setCustName("Thrifty Food"); cust.setShipToStreet("1 Diet Lane"); cust.setShipToCity("Fasting"); cust.setShipToState("IN"); CustomerFrame1 cf1 = new CustomerFrame1(cust); } Added Changed CustomerFrame1 ▮ Let’s test that customer object is accessible ▮ In CustomerFrame1's constructor, add code to display customer name in the console then run as app System.out.println(customer.getCustName());

59 Chapter 4© copyright Janson Industries 201459 Frame is displayed Name is displayed in the console Make sure window close button works

60 Chapter 4© copyright Janson Industries 201460 CustomerFrame1 ▮ Add labels to hold info ▮ Add button to frame using VE ▮ Define button properties ▮ Including good names ▮ Tie button to ActionListener ▮ Code actionPerformed to retrieve info from Customer object and put in labels

61 Chapter 4© copyright Janson Industries 201461 Copy the Label definition statements from CustFrame into CustomerFrame1

62 Chapter 4© copyright Janson Industries 201462 RAD inserts the import statement

63 Chapter 4© copyright Janson Industries 201463 Delete the System.out.println statement and set the layout to null

64 Chapter 4© copyright Janson Industries 201464 Switch to the design pane Notice the labels are not on the frame

65 Chapter 4© copyright Janson Industries 201465 Drag/drop the four labels Increase the width of the labels and the height of the frame to 250

66 Chapter 4© copyright Janson Industries 201466 Define the button properties so it looks like this Click on the AWT button icon Click on frame to add the button

67 Chapter 4© copyright Janson Industries 201467 Let’s test: add to the actionPerformed method: System.out.println("listener is working"); run the CustomerFrame1, click the Display button For more complicated GUI components, RAD creates a get method that sets all the properties Add the actionListener to the button in the get button method

68 Chapter 4© copyright Janson Industries 201468 Click Display button Text should appear

69 Chapter 4© copyright Janson Industries 201469 CustomerFrame1 ▮ Code actionPerformed to retrieve info from Customer object and put in labels ▮ Use Customer object’s getters and concatenation to build label text ▮ Can cut and paste CustFrame code into actionPerformed to overwrite the println statement

70 Chapter 4© copyright Janson Industries 201470

71 Chapter 4© copyright Janson Industries 201471 Must change cust. to customer. Can't do Replace All because main uses cust.

72 Chapter 4© copyright Janson Industries 201472

73 Chapter 4© copyright Janson Industries 201473 Run, click Display Why does data appear like this?

74 Chapter 4© copyright Janson Industries 201474 public static void main(String[] args) { Customer cust = new Customer(); cust.setCustName("Thrifty Food"); cust.setShipToStreet("1 Diet Lane"); cust.setShipToCity("Fasting"); cust.setShipToState("IN"); CustomerFrame1 cf1 = new CustomerFrame1(cust); } Only these were set CustomerFrame1 ▮ In main, the test data wasn’t complete ▮ Only some properties were set

75 Chapter 4© copyright Janson Industries 201475 Change CustApp to create a CustomerFrame1 object Run CustApp

76 Chapter 4© copyright Janson Industries 201476 Click Display button Notice that the JVM automatically redisplays frame after actionPerformed method

77 Chapter 4© copyright Janson Industries 201477 New Customer Example CustApp Customer Object CustomerFrame1 Object Creates Customer object, assigns to variable c, sets object’s properties Creates a CustomerFrame1 object, passes c, assigns to variable cf Retrieves & displays Customer object properties Non-graded assg: Do it!

78 Chapter 4© copyright Janson Industries 201478 Non-graded Assg ▮ Export as one jar file ▮ Customer.java ▮ CustomerFrame.java ▮ CustomerFrame1.java ▮ CustApp.java ▮ Email the jar file as an email attachment to wsjavaws@yahoo.com


Download ppt "Chapter 4© copyright Janson Industries 20141 More GUI ▮ Events, Actions, and Listeners ▮ The Visual Editor ▮ More GUI component properties Non-graded assg."

Similar presentations


Ads by Google