Chapter 4© copyright Janson Industries 20141 More GUI ▮ Events, Actions, and Listeners ▮ The...

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

If you can't read please download the document

description

Chapter 4© copyright Janson Industries 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 {

Transcript of Chapter 4© copyright Janson Industries 20141 More GUI ▮ Events, Actions, and Listeners ▮ The...

Chapter 4 copyright Janson Industries More GUI Events, Actions, and Listeners The Visual Editor More GUI component properties Non-graded assg part1 part2part1part2 Chapter 4 copyright Janson Industries 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! Chapter 4 copyright Janson Industries 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 { Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries 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); Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries WindowListener Create a separate class to act as the window listener Must import WindowEvent and implement Window Listener Chapter 4 copyright Janson Industries WindowListener Create the window listener object Add the window listener object to the frame Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries 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) Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries New Customer Example CustApp Customer Object CustomerFrame Object Create a Customer object, assign to variable c, set objects properties Create a CustomerFrame object, pass c, assign to variable cf Retrieve & display Customer object properties Chapter 4 copyright Janson Industries Visual Class Click File, New, Visual Class Chapter 4 copyright Janson Industries Specify CustomerFrameUse the Style box to specify Frame as superclass Have RAD generate main and window event handling methods Implement by clicking Add Chapter 4 copyright Janson Industries Start typing WindowListener, select WindowListener from the AWT package, click OK Chapter 4 copyright Janson Industries Will use main method for testing. Click Finish. Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries Design pane Source pane Resize Open Component Palette Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries 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 Make frame visible and layout null by adding this source code into initialize Or change in Properties view Chapter 4 copyright Janson Industries Visual Editor this.setLayout(null); this.setVisible(true); Chapter 4 copyright Janson Industries Click Label in palette then click on design pane to add Should give the Label variable a meaningful name Chapter 4 copyright Janson Industries Or accept the default Label's source code and property values are displayed Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries Specify new name, click OK, and save all modified resources VE changes all the source code Chapter 4 copyright Janson Industries Chapter 4 copyright Janson Industries Labels Lots of label properties Foreground and background colors Size, Location Font style Alignment Some nice VE features to define labels Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries Click Marquee and pointer changes from arrow to +...to here, so all components are inside the dotted line box Click here and drag... Ive added 3 more labels Chapter 4 copyright Janson Industries When released all components selected, any changes will change all Alignment and size changes according to the anchor component Alignment button brings up Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries Changed pointer to Selection, then reselected so oldest label is anchor (held CTRL key, clicked each label) Click left align Chapter 4 copyright Janson Industries Resize all by dragging any label resize handle Chapter 4 copyright Janson Industries Need to align labels vertically Click distribution box then distribute vertically Chapter 4 copyright Janson Industries Distributes evenly within the box. Make distribution box larger, distribute vertically again Chapter 4 copyright Janson Industries Drag labels to center on frame Click distribution box button again (to deselect), then close Customize Layout window Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries 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; Chapter 4 copyright Janson Industries 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: Chapter 4 copyright Janson Industries 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. Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries 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; Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries CustomerFrame1 Create a visual class CustomerFrame1 Implement both action and window listeners Has a button that when pressed will display the data Chapter 4 copyright Janson Industries CustomerFrame1 Initially displayed with no data Click Display button Chapter 4 copyright Janson Industries CustomerFrame1 info displayed in labels Chapter 4 copyright Janson Industries Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries CustomerFrame1 Notice that the window methods dont 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 Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries Click Frame to select and display properties In properties pane, click visibles drop down button and select true Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries 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); Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries public class CustomerFrame1 extends Frame implements ActionListener, WindowListener { private Customer customer = null; : : : : public CustomerFrame1(Customer cust) { super(); customer = cust; initialize(); } : : : : } CustomerFrame1 Added Chapter 4 copyright Janson Industries Get an error: why? How fix? Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries 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 Lets 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()); Chapter 4 copyright Janson Industries Frame is displayed Name is displayed in the console Make sure window close button works Chapter 4 copyright Janson Industries 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 Chapter 4 copyright Janson Industries Copy the Label definition statements from CustFrame into CustomerFrame1 Chapter 4 copyright Janson Industries RAD inserts the import statement Chapter 4 copyright Janson Industries Delete the System.out.println statement and set the layout to null Chapter 4 copyright Janson Industries Switch to the design pane Notice the labels are not on the frame Chapter 4 copyright Janson Industries Drag/drop the four labels Increase the width of the labels and the height of the frame to 250 Chapter 4 copyright Janson Industries Define the button properties so it looks like this Click on the AWT button icon Click on frame to add the button Chapter 4 copyright Janson Industries Lets 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 Chapter 4 copyright Janson Industries Click Display button Text should appear Chapter 4 copyright Janson Industries CustomerFrame1 Code actionPerformed to retrieve info from Customer object and put in labels Use Customer objects getters and concatenation to build label text Can cut and paste CustFrame code into actionPerformed to overwrite the println statement Chapter 4 copyright Janson Industries Chapter 4 copyright Janson Industries Must change cust. to customer. Can't do Replace All because main uses cust. Chapter 4 copyright Janson Industries Chapter 4 copyright Janson Industries Run, click Display Why does data appear like this? Chapter 4 copyright Janson Industries 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 wasnt complete Only some properties were set Chapter 4 copyright Janson Industries Change CustApp to create a CustomerFrame1 object Run CustApp Chapter 4 copyright Janson Industries Click Display button Notice that the JVM automatically redisplays frame after actionPerformed method Chapter 4 copyright Janson Industries New Customer Example CustApp Customer Object CustomerFrame1 Object Creates Customer object, assigns to variable c, sets objects properties Creates a CustomerFrame1 object, passes c, assigns to variable cf Retrieves & displays Customer object properties Non-graded assg: Do it! Chapter 4 copyright Janson Industries Non-graded Assg Export as one jar file Customer.java CustomerFrame.java CustomerFrame1.java CustApp.java the jar file as anattachment to