









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of containers and components in java programming, using the example of a program that displays name popularity over time and manages an album inventory. Containers are objects that hold other components, while components can be resizable and generate events. The document also covers the implementation of the componentlistener interface and the creation of a simple album class to store album information.
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Programming Methodology-Lecture
Instructor (Mehran Sahami): Howdy! So welcome back to yet another fun-filled, exciting day of CS106a. We’re getting close to that Thanksgiving recess, which is always a good time. In the days of yore, it used not be a whole week. It used to be you got like one or two days off. You got like Thursday and Friday, which means you would have gotten only one day off from this class and now you get a whole week to mellow in style or catch up on all your other work as the case may be.
So there’s one handout today. It’s Assignment No. 6, which is the next assignment, name, circle, watch. They talk a lot about that assignment in this class today. As a matter of fact, we’re very fortunate for coming today because I will give you so much gratuitous information that presents that assignment. It’s just almost laughable.
And then Assignment No. 5, as you know, is due today, so we can do painful to see how much time it actually took you. Just wondering, I heard from my little sources that exist was that last night, a lot of people were at the Lair and the computers at the Lair. Just wondering how many people were at the Lair last night. A fair number. And some of the computers at the Lair just mysteriously didn’t have Eclipse anymore. Anyone run into that problem? One person, okay.
Yeah, there was some reimaging that went on, which I had nothing to do with and didn’t know about until some of the section leaders said, hey, Eclipse is missing on these machines and, you know, so I sent a very anxious email, I should say, to academic computing, and there had been some mix up and Eclipse is now reinstalled on all the machines, so I’m glad it didn’t affect too many people, but if it affected you, I apologize, although we really didn’t have anything to do with it, but I’m sorry that you had to experience that pain.
So in terms of the painful, how much time it actually should give you the assignment on the Yahtzee assignment? Just wondering, how many of you actually liked the Yahtzee assignment? Just a quick show of hands. And how many thought it was just more pain than it was worth? Anyone? You folks, all right. And that’s good to know. I always try to
So anyone in the zero to two-hour category? You just did it and it worked and life was good. Two to four? A couple people; that’s always good to see. Four to six? A reasonable contingent. Six to eight? Good times. Eight to ten? Wow, pretty healthy distribution there. Ten to twelve? Okay, we’re falling off a bit. Twelve to fourteen? You folks. Fourteen to sixteen? Maybe a couple. Sixteen to eighteen? Eighteen plus? Taking a late day? Yeah.
As you may have read – I should refresh your memory because it pertains to Assignment No. 6, but as you may have read on Assignment – on Handout No. 1 at the very beginning of class, because Assignment No. 7 is due the last day of the quarter, you can’t use late days on that assignment. So if you have late days and you’re like, hey, I was just
saving them up for No. 7, well, seven’s due the last day so there’s no classes beyond seven and you don’t say, hey, I’m taking a late day. That gives me an infinite amount of time to turn it in because there are no more classes. No, it doesn’t work like that.
So Assignment No. 7 you can’t use late days on. So again, I would discourage you from using your late days, but should you have them and you’re like, hey, I really wanna use my late days, Assignment No. 6 is kind of your last chance to actually use your free late days. Again, not that I would encourage you to use them, but just so you know up here that you don’t get Assignment 7 and say, hey, I thought I could use my late days for this. You’re getting plenty of advanced warning that you actually can’t.
One other quick announcement; CS Career Panel is today, so after today, I’ll stop mentioning it because I believe most of you can’t actually time travel, so today, 5:30 p.m. in Packard 101. Be there. We have a stellar panel of folks from academia, from industry, a lot of people who actually – every one is a computer science major and four of the five people graduated from Stanford’s department. The fifth is actually a faculty member here that didn’t graduate from my department, but is a faculty member here.
And you’ll actually see there’s a very broad range of stuff to do. Out of those five people, though, actually, I think only one, maybe one and a half who actually do programming as their sort of fulltime job. But all of them are computer science undergraduates, so you get sort of a breadth of what computer sciences can do.
So I’m so happy to see the fifth region. You’ll see the variance gets larger, right. As the quarter goes on, that kind of happens, but it’s good to see that the mean is still where we generally want it to be.
So now it’s time to talk a little bit about your next assignment. You just turned in your last assignment. You’re like, no, no, it’s not time to talk about the next assignment. I’m still working on the last assignment. Well, just pay attention.
Here’s what you’re going to do for your next assignment. It’s a little program called NameSurfer, okay? And the idea of this program is it turns out that the government actually keeps track of statistics of the popularity of names over time, okay. So every decade when they do the census, they kind of keep track of the number of names and they rank the top 1,000 names in terms of popularity for boys’ names and for girls’ names, and they actually make this data available. We thought wouldn’t it be cool if you actually were to display names to the user in terms of how that popularity changed over time.
So that’s what NameSurfer does. Here’s the program. What it does is it shows you the decades. You can notice that this is a graphics, or involves graphics, right, has the decades from 1900 up to 2000 and so relatively recent date up to the last census. And what we can do is we can type in a name and then click graph, and what it will do is it will show the rank of that name over time, on a decade-by-decade basis. So let’s try Fred, for example. If you graph Fred, you can see Fred real popular at the turn of the prior century and then Fred just had this quick decent into oblivion, where now he’s the 974th
works so you can try out typing in different names and see the behavior. But it doesn’t resize because its size it stuck by the browser and changing the browser window doesn’t actually change the size of the applet. That’s just life in the city, okay? But that’s the way NameSurfer works.
So what we wanna think about is actually building programs that have this kind of property and to understand how programs that have this kind of property work, what we need to think about is something known as a component and related to that is a container. So first of all, I’ll show you all the classes in Java that pertain to the components and containers. Not all of them, but a good number of them. So here’s the classes in the component hierarchy and some of these will look familiar to you. So programs of which Console program and Graphics program that you’ve been using the whole time are j- applet, which is an applet, which is a panel, which is a container, which is a component.
So somewhere all the way up the food chain, right, the food chain before we didn’t go that high. We just went up to applet when we showed you sort of this picture before. Turns out all your programs are components and containers. Every component is a container, or every container is a component. And so you may ask, well, what are containers and components? So the way you wanna think about this is at some generic level, a component is anything that can appear in a window. It’s anything that can in some sense be displayed. So your programs are appearing in a window that’s getting displayed. That’s why they’re components, okay? Now, that also means that a graphics program and a console program are also components because they’re subclasses of program. Now at the same time, there’s a notion of a container. What’s a container? A container is simply something that contains other components, so a container just contains a set of components and you might say, hey, Mehran, that’s really weird. A container is a component, but you’re telling me it contains a set of components? What’s that all about? And the simplest way to think about it is it’s this, okay? This is a component, this bag. It turns out this bag is also a container because it contains things and it’s perfectly fine for something to be a container and also to be a component.
So here’s what I mean, right. Your programs, as it turns out, are all components, which means they display in the screen, but they’re also containers because your programs contain other things. For example, in the display of your program, it may contain a button. Right? It may also contain, say, a text field. And the other thing that it may contain, if you have a graphics program besides these kind of components, which buttons and text fields are also components – I’ll show you an example of that in just a second – it may contain another container. And it’s perfectly fine for containers to contain containers. As a matter of fact, you’ve been using them for the whole time. Your graphics program, especially if you have a graphics program that has some interactors on it. It could have buttons. It could have text fields. It could have a G-canvas, and gee, guess what? A G-canvas is a container and a component. Because it’s a component, it can be contained inside some other container, and because it itself is a container, it may have certain things in it.
Oh, like here’s our friend, the frog image from the mid-term. Yeah, that was just something that was in another container, okay? So there’s no reason why something that is a container can’t be a component, which means it can be contained in some other container, that’s fine, and all containers themselves are components, so they can be stuck in somewhere else. That’s the basic idea. You should think of it as bags and you can put bags inside of other bags, and bags contain things. So even though you see the hierarchy this way, the concept itself is not actually that difficult. Now, the thing you wanna think about this is I just mentioned to you your programs are actually components and containers. G-canvas is a container and a component, but components that we talked about before, j-components, like our friends, say, oh, j-button – you actually saw this slide a couple classes ago, j-button or j-label or j-combo box, all these things you saw before – are actually j-components. They all are subclasses of j-component, which means by being subclasses of j-component, they were also containers, strangely enough, and they are also components. We just think of them generally as components and we try to put them in other containers, like we put them inside the display for a program and that’s its container. But that’s the general idea that’s kinda going on here. So when you wanna think about containers and components, what you also wanna think about is that these components themselves, before we thought about like action listeners, right, like something happens like I click on some component and some particular event happens, like I clicked on it, something like that. We can think of the whole component as also listening for other events because a component is something that display stuff on the screen.
Stuff can happen to be components, like they can be resized and when a component’s resized, some event can get generated and sent to that component that says, hey, guess what? You got resized. You need to do something. Like the canvas that shows all the graphics, it can get some event that says, hey, canvas, yeah, you were just resized. Do whatever you wanna do. Up until now, you didn’t do anything. Someone came and resized your canvas. You said, yeah, you’re resizing my canvas. All of the pieces I have in my canvas stay where they are. Nothing’s changing. Now what you’re going to get is the ability to have the canvas be told, hey, you got resized or something else happened to you. You can respond to it however you want, which means you can redraw everything that’s in your canvas, so that, for example, you can have all the layout come out just fine when people resize your window. So what does that actually mean in terms of these components and containers? What it means is that we can have listeners in the same way that you kinda thought about listeners from mouse events and listeners for j-components. You can have listeners for components, okay, like we talked about resizing the window.
Now, here’s a little simple example of something that can go on with one of these components. So I’ll come back to Eclipse, I’ll run a little program here – I gotta compile
want to give it to you now because then you wouldn’t pay attention to what’s going on on the screen, okay? So what I’m gonna do is I’m gonna have a class My Canvas. And My Canvas is going to be a G-canvas, so it’s going to be an extension of a G-canvas. It’s gonna extend G-canvas. So all My Canvases are G-canvases. Because it’s a G-canvas, that means it’s a component. Because it’s a component, I can add it to a container. So that’s why add works there.
Now, what am I gonna do with this thing? The other thing I wanna do is I wanna say, hey, this is a component that’s gonna listen for stuff, and so the way I do this is rather than doing add mouse listeners like I did before, or add action listeners, which look at actions of things like buttons, the way a component actually listens is it implements the component listener interface. So when you wanna have some component that is actually gonna get events that it’s listening for, component events, the way you do that is when you create the class, you say it implements component listener. That’s just the way you say it, so it’s going to implement the interface. There’s some set of methods it needs to provide that implement that interface and I’ll show you the list of methods real quickly. They’re just down here. These four methods here, component resize, component hidden, component moved, and component shown are the methods that comprise that interface, the component listener interface. So anything that implements the component listener interface needs to provide these four methods. In a lot of cases, you won’t necessarily care that something happened and so, for example, if the component is hidden, which means some other window comes over it, the component is moved, or the component is shown, you’re like, yeah, whatever, like I don’t care. Those are good things. I need to have methods that correspond to those things because in order to implement the interface, I need to provide methods that correspond to all the methods that the interface is expecting, so I need to have these. But in three of these four cases, I’m not going to do anything, right? It’s just open brace, close brace.
Someone comes along and says, hey, guess what? You know you were occluded. Someone hid you, so your component’s hidden, and you’re like, yeah, that’s cool. You’re like, aren’t you going to do anything? No, I like hiding. And it’s like, oh, look, your component was moved. Someone came along and moved your window. Aren’t you gonna do something? And you’re like, no. No, I’m cool with that. That’s fine. You moved me, all right? I won’t get into the music, but if you can think of that, what that reference actually is, it’s not that difficult. Louie, Louie. Component shown, again, definitely, so the only time we actually care about doing something is when we’re resized because when we’re resized, we wanna say, hey, our component was resized. Let me move that square to wherever the new center is of that window. So whenever my component’s resized, I’m gonna get the component resized method called and I’m just gonna call the method called Update. And what’s Update gonna do? Update’s right up here. All Update says is remove all, so who’s the remove all message being sent to? It’s being sent to myself because I’m not specifying any particular object to send this message to. So when I say remove all, I’m sending it to My Canvas. Well, My Canvas is a G-canvas, which means remove all, removes everything that’s on the canvas. It clears the canvas, which means wherever the square was before, it just got cleared.
And what I’m gonna do is I’m gonna add some rectangle, and I’ll show you the rectangle. It’s pretty simple. So what I have is I have a private G-rect that I call rect. It’s just gonna be some private variable that I keep track of. In the constructor for my class, what I’m going to do is I’m gonna – before I tell you what add component listener this does, I’m going to create a new rectangle, who’s width and height are whatever I specify as constants is the box width and box height. They’re just constants, not a big deal. And I set it to be filled. So what I get is a filled square. I created this rectangle as a filled square. I haven’t done anything yet with it other than create it, but I’ve created it and I have some variable rect that refers to it. So all Update does is it says, hey, you know what? Remove the square if it was anywhere on the screen before and now add that rectangle back at the center location of the square, right? What’s the center location of this point? You should be good, real good, with figuring out how to center stuff on the screen. You take the width of the screen, subtract off the width of the object you’re displaying, and divide by two. That’s the x location. You take the height of the screen, subtract off the height and divide by two. That’s the y location. And this get width and get height will get the current size of the component, which means when the components been resized, this update method gets called, it closed the screen, and it gets rid of the old box and draws a new box at wherever the new center of the screen is because this gets called, which means Update gets called after the component’s been resized. So that way it’s gonna redraw the square always in the center of the screen.
The only other thing about this program that you need to know is in the constructor for My Canvas, My Canvas implements the component listener, so you not only need to say, hey, I implemented the component listener, but you need to let the – you need to actually say, hey, you know what? I am a component listener, so add me to the component listener list, and the way it does that is you just call add component listener and you pass this, which means myself. So somewhere, I say, hey, you know what? Add me to the list of component listeners. I’m giving you a parameter to myself so you know who I am, so when something happens to me, you can call my component resize method to get resized. And that part up there, that Add Component Listener, is just a broiler plate, but it’s gonna, for example, show up in your NameSurfer assignment because guess what? In your NameSurfer assignment, when your component gets resized, you’re going to do something very similar to this. You’re gonna find out what the new dimensions of the screen are and redraw everything that should actually be on the screen, which means you need to have some way of being able to keep track of the stuff that’s on the screen. So any questions about this? All righty, so that’s the basic notion of this idea of a component listener, okay, and what we wanna think about next is how do I create a larger program with these kind of things. So this is just the simple hint. Now I’m gonna give you something that’s even a bigger hint to what’s going on in NameSurfer.
And so what I’m gonna do is one thing that’s real popular these days is kind of like music, right, like online music and albums and the whole deal. So what I wanna do is implement a program that keeps track of data related to music and at best, I’m gonna have a little music store, the program for the music score. And the basic idea here is that a lot of what – well, I shouldn’t say a lot, but so far you’ve been – I don’t know. You did a lot of games in this class and games are great and I totally encourage games, and I’m a
that doesn’t exist, like I might type in – I don’t know – cow, which is – there’s probably an album out there called Cow. I don’t know why I always pick Cow. I just do. In which case, it definitely disappears. Like we don’t have that in stock. Just back off, man.
So that’s what’s going on in this program. So we need to be able to resize the bars and we need to vertically center the stuff we display in the screen. How do we actually do that? So one thing we wanna keep track of is you wanna go back to the data, right? You actually wanna say if I need to do this, in order to display anything, the first thing I need to do is figure out some information about the data that I wanna store. So what data do I wanna store? Well, I probably wanna store some information about albums because that’s what I have in my data file. So if I think about my data file and wanting to encapsulate all the information about one album, okay, I wanna do something on a per album basis because every album is gonna have, for example, an album name. It’s gonna have some band name that produced that album. And some number that’s in stock, like Num Stocked. Oh, and this is a string and this is a string and this is an int and it’s good to put semicolons at the end of them and it’s probably a real good idea to take this whole thing and turn it into a class that’s gonna encompass all this because what I want to have is modularity in terms of my data. If there’s a whole bunch of data I wanna manage, the first question I wanna ask myself is what’s the basic element of data that I want to have? In this case, the base element is one album. I’m gonna have a bunch of albums, but the basic idea is one album and an album’s gonna contain an album name, a band name, and a Num Stock, which are, for example, gonna be private fields of that album.
So one of the things I might wanna do and I’ll just show this to you real quickly, is I would create album.java, which is just some class. It doesn’t extend anything. It’s just gonna be a class to keep track of data and what it’s gonna do is it’s gonna have some private instance variables, album name, band name, and Num Stock, that’s gonna keep track of the information from one album. And then it’s gonna have some other methods. One of the methods it’s gonna have is the Constructor. What does the Constructor do? It says, hey, you want an album object? Give me a name for the album, a name for the band, and a number of stock. I’ll set all my internal variables to those things. I’ll do a little annoying pop-up on the screen and what you’ll get from your constructor is you’ll get an album object that encapsulates that information. Now, I made all this stuff private, right, album name, band name, and Num Stock, because what I wanna do is when you create the album, you say, hey, this album’s in stock. I don’t want you to go back and say, hey, no, no, no, the name of that album changed. That’s not what happens, right? When an album’s released, the name stays the same, generally. We won’t talk about special cases. So in order to get the information out of an album object, I need to have some getters. I can potentially have setters as well, but here I have some getters. Get album name, get band name, and get Num Stock. Pretty straightforward getters. You’ve seen getters in the past; not a whole lot of excitement going on there.
The only other thing I might add, which I told you in the days of yore that you didn’t have much need for until now, is to have a 2-string method, just returns a string representation of that particular object if you ask for string representation. So it just writes out “album name” by band name colon the number stock in stock. So it can return
to you a string that basically has all the information about the object in some nice little display format. That’s the whole class, okay? This is just a simple class that keeps track of information about one album because we’re gonna have a bunch of albums that we wanna keep track of. So now that we can keep track of information about one album, it kind of brings up a deeper question. The deeper question is we don’t wanna keep track of just information about one album. We, in some sense, wanna keep track of a whole set of albums, a database of albums, and what we wanna do if we think about a running program – so I’ll go back to the running program – is we wanna allow someone, given a particular name for an album, to be able to go look up all the information on that album. So the way you wanna think about it is given a name, I wanna go and look up the record for that particular thing. And this happens all the time. It happens with your student I.D. records, except we don’t do it by name. We use student I.D. reenact records. We say given your Stanford student I.D., let’s go grab a record of all of your information. It’s exactly the same problem. This is just the simple version with albums. So if we wanna think about having some mapping, where, for example, from some name, we can go get the whole information about that record. What kind of data structure might we think about? What kind of thing that you’ve seen before?
Ah, yeah, the whispered HashMap. Could it be the HashMap? And in fact, yes, it could be a HashMap. And the thing you wanna think about with the HashMap, that’s very concentrated. It’s a low variance event where it’s kind of tougher the whole time and you’re like, ah, everyone. Yeah, it’s early. It’s before Thanksgiving. Have some sugar. We can think about a HashMap. Now when we think about having a HashMap – suddenly, everyone wakes up. Food? Food? Bark, bark. I know, sometimes. Anyway, HashMap, what are we gonna map to? What are gonna be the two types that our HashMap is parameterized by? What’s the first type? What are we gonna look things up by? What’s the key? String, right? We’re gonna look things up by the name, which is a string. When we look something up, what do we wanna get back? An album. So what we’re gonna have is a HashMap that maps from strings to albums, and I think someone said album right over here, although I think I missed you. Sorry. I missed you again. So the basic idea is given some name, I’ll go look up the whole album and at this point, you might say but Mehran, isn’t the name inside the album? Yeah, that’s fine. In your student record, it also contains your student I.D. and your name. We just happen to look it up by your student I.D. and it’s the same thing here. The user’s gonna give us the name to look it up. When we go look it up, it’s fine if the name’s also contained in the album, but there’s a whole bunch of other information we care about there as well. So if we have some HashMap, we’ll call this particular HashMap “Inventory” and we might say new HashMap that’s gonna map from strings to album, and there’s the constructor. And so that’s how we might actually create this particular HashMap.
So once we have this HashMap, if we’re gonna have some object inventory that’s a HashMap of all these things, we need to load it up. We need to say, hey, all my data is actually sitting in a file somewhere. What I need to do is read the data in from the file and as I read the file line by line, every line I’m gonna create one of these album objects because every line is information about one album, and after I create this album object, I’m gonna add it to my HashMap and so my inventory’s gonna be all of my albums
me the index of the bracket where the band name starts and I add one to that, which gets me to the first character of the band’s name.
And then I do the same thing over here. Where’s the end of the band’s name? Get the index of the closing bracket starting at the end of the album name so I don’t get the closing bracket for the album name. I get the closing bracket for the band name. So I start at the album name plus one. So now I have the boundaries for the band’s name and so the band’s name is just the substring I get in the line from the starting index for the band’s name to the ending index for the band’s name. Now, the final thing I need to do and this is funky thing. It’s probably the funkiest thing of this whole function, of this whole method, and it’s pretty simple, which is say that last thing that’s on the line is actually integer, so I don’t wanna pull it out as just a string. I need to actually convert that string to a real integer. How do I do that? Well, let me first find out where that number actually lives. Where do I find the number? I look for the number by finding the first space after the end of the band name. The first space after the band name is gonna be this index right here. The number starts on the next location, so if I take that particular location, after the end of the band name, and add one to it, I now have an index that’s the very beginning of the number of that numeric sequence. So what I wanna do is I wanna say pull out that numeric sequence as a string and convert that string to an actual integer. How do I do that?
Well, the way I pulled it out is I say take the substring of the line that starts at the starting position of the number of the number stock. That’s where I just computed in the last line was where that number starts. Because I don’t specify an ending index for this substring, it goes till the end of the line, which means it takes all the characters. If there happens to be like 100 in stock, it starts at the one and takes the 100, it takes that whole substring as this substring right here, and what do I do to that? There’s a method that’s a static method of our friend the integer class called parse ins, and so if you say integer.parseins, you give it a string, it converts that string into its integer equivalent. So what it does is it says, hey, I’m giving you the number that’s the number stock as a string. It says okay, I’m gonna parse that and turn it into an integer and what I will give you back is something you can store as an int. So that’s how we convert that last portion of line from a string to an integer. We first extract it and we get the integer. And then what are we gonna return to the person who called our function? We’re gonna return a new album object, we’re gonna create a new album object, where we initialize the album object to have the album name, the band name, and the number stock that we just parsed out of that line. Are there any questions about that? Standard thing you do in files, you pull out a line as a string, you break up that string using some string manipulation operation, however you wanna break it up, and then you potentially create some object out of it so that you can store all of the nice things that you extracted out of the string into nice little name fields.
So this gives you an album object. Then back up here, that album object is getting returned by parse line. We put those album objects into our inventory HashMap indexed by the album name. Are there any questions about that? If that’s clear, nod your head. Good time. All right. And that’s basically the role loading of the database. Now the only other things going on here is we need to figure out our little interactors, right? So in our
program, we have a label that asks for the album name where the user can type the album name, so we put in a label. Then we put in a text field that has a maximum size of 20, right? At this point, this should all be sort of old hat. We add the label to the southern region. We add the album name, which is just a text field that’s gonna take in the album name for the southern region so we get those two interactors. We get the label and the text box, okay? And the things we wanna do after we set up the interactors is we say, hey, I need to display the stuff somewhere. Mehran, I remember he told me, oh, about 20 minutes ago, that if I wanted to actually have some canvas that could resize itself automatically when the user changed the window, what I need to do is extend the G- canvas. I need to create in my own version of canvas and make it a component listener. That’s the same thing I’m gonna do here. I’m gonna create something called a Music Shop display and Music Shop display, which I’ll show you the class for in just a second, is something that I’m going to store as my canvas as a private variable inside my class. And I’ll just go ahead and say, after you put your little interactors on this screen, create your new canvas and add the canvas, just like I did before in the previous program, now load all your inventory, so go read the file, do all that parsing, the funky stuff, set up the inventory. Add action listeners because I wanna be able to listen for events that actually happen on the buttons and also add the text field album name as an action listener. So it basically wires everything up. It says put in the interactors, create the canvas that I’m gonna draw stuff on, load the inventory into data, and get ready to listen for stuff. And then it’s ready to go. It’s not gonna do anything until I get some events, but now it’s ready to go. So load inventory you saw, parse line you saw. The only thing you haven’t seen is what do I do when an action’s performed.
I don’t have some button the user compressed. All I have is the text field, so when they hit enter, I check to see if the source is the text field album name. If it is, what I’m going to do is ask the canvas to display the inventory of a particular album. How do I get the particular album? What I do is I’m gonna say what I want you to display, I’m gonna give you an album object that’s gonna contain all the information you need to display, so I’m gonna call display inventory pass in album object. How do I get the album object? I say, oh, text field, give me the text that’s in you. It says, oh, here you go, and here’s what these are typed in. That should be the name of an album. I can use that name for an album in a HashMap to look up the album object, so in my inventory HashMap, I say get on the album name and if there is an album object in my inventory HashMap, that’s what I’ll get back. If there isn’t, I’ll get back null and I’ll go ahead and call display inventory with null, so it needs to know how to handle that. So all the action to do the display is gonna happen in display inventory. So let me show you musicshop.display, which kind of pulls this final thing together. These are shock display extends G-canvas, just like you saw in the previous example with the little square, implements component listener, just like you saw in the previous example, and it just has a little bit more complexity. The only additional complexity it has is in its constructor. As before, it needs to add itself as a component listener. That’s broiler plate. But it says, hey, you know what? I’m gonna keep track of the last album the user actually typed in because when you resize the window, what I need is the information about that album to redraw everything in the window relative to that last album. So when I start off, there was no last album, so I set it to null, but as soon as you give me a real album, that’s what I’m gonna store in last
variable, so that when I resize, it knows what information to redisplay on the screen to resize. Any questions about that? We’ll do exactly the same thing in NameSurfer and I’ll give you all the code you just saw.
[End of Audio]
Duration: 50 minutes