I hope the title wasn't too confusing. First I will start with this week's requirements, then the code I have so far. Again I am just looking to get pointed in the right direction.
Requirements for Week 7:
• Modify the Inventory Program to use a GUI by either
1. Adding a new class that extends JFrame (example in Chapter 11), or
2. By creating a new JDeskTop Application project.
• Use an ArrayList or other automatically resizable collection instead of an Array to store your products.
• The GUI should display the information of the first product in your collection, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.
• In addition, the GUI should display the value of the entire inventory. Since this requires a loop of your collection, out this functionality in a separate method of your GUI class. This will allow you to call it only when updating the total is required.
• At this point, all your main method should do is create an instance of your GUI class, set the Window size, setDefaultCloseOperation to EXIT_ON_CLOSE, start the GUI by calling it’s show method and get out of the way. (Desktop app will do this for you).
• The Collection of products now belongs to the GUI class as a class level variable. This will allow all methods in the GUI class to have access to it.
• If you use the Desktop Application, it will create a read-only method named initComponents. As you drag components to the screen, initialization code for them will be added to this method. The method is then called from your GUI constructor. Things you add manually, like your collection cannot go in this method. You need to initialize them in the constructor yourself.
Do not use a series of pop-up dialog boxes. A GUI is a single screen that gives you access to all of the applications functionality. Pop-ups can be used for prompts, and when needed to collect specific data. But if you need values for 4 variables, use a single pop-up with four fields, not one popup per variable.
Post a zipped copy of the entire project.
I know, quite a bit to do here!
The last piece of coding is the source created from using Netbeans 7.2 GUI builder. From here, I am unable to figure out how to get 1 instance (I can remove the Item select box and text if it will make life easier) of the data stored in the Inventory program to display in the GUI, let alone even run the GUI. Any help would be greatly appreciated.
Dan D'Apice wrote:OK, post has been re-edited. I put comments where the code was describing what I did. Hope that works. If not, let me know and I will try to adjust it.
You missed a few, but I've fixed them by breaking them up. Do you see how much easier your thread is to read now?
The line limit here is around 90-100 characters and, to be honest, having them longer makes your code difficult to read anyway, so it's not a bad habit to get into. Personally I use 80.
But thanks for your efforts.
Joined: Sep 26, 2012
Thanks again, Winston. I actually made a note to post ~80 characters per line in the future. Now, if I can just figure out how to get my array list incorporated into the GUI, I should be good. I'm not focusing on the drop box right now, just trying to get an instance of my array list into their respected text boxes...
I am unable to figure out how to get 1 instance (I can remove the Item select box and text if it will make life easier) of the data stored in the Inventory program to display in the GUI, let alone even run the GUI.
I don't really get what issue exactly are you having.
The instructions you got are pretty clear:
Use an ArrayList or other automatically resizable collection instead of an Array to store your products.
The Collection of products now belongs to the GUI class as a class level variable. This will allow all methods in the GUI class to have access to it.
So what you're actually required to do is to have a field as part your gui class, that will be the above mentioned collection (say ArrayList). That collection will store your data, or in this case your inventory (books).
Having that done, you can go further and implement details displaying.
First of all, that component you are using is not "select box", but JComboBox. When you initialize your GUI you would need to populate that combo box (jComboBox1 in your code) to contain all the items stored in the collection. One approach is to actually add objects of your Book class to your combo box:
But also note that what will be displayed to the user as items in combo box in this case is the result of toString() method of your Book class, so it would be good to modify it to return just basic information (e.g. just item name).
Now you just need to add action listener to your jComboBox1 to trigger when the user changes the selection in combo box:
The quieter you are, the more you are able to hear.
Joined: Sep 26, 2012
I actually took out the JComboBox and text to keep things simple (I hope). What I want to do is just populate the text fields of the GUI with their respected data. E.G. itemNumber would be placed in the Item Number text field, itemName would go in the Item Name text field, etc. After that, I need to display the total value of the item (inventory * price each) and display that in the Value of Item Stock text field. Finally I need to calculate and display the total for the entire inventory (all three of the array list) and display that in the Value of Entire Inventory text field. I understand how to set up listeners for each text field, but getting that data to the text field is where I am stumbling. You may have explained it in the last post, but I am not sure if I have to create another constructor and array list to bring the data over to the GUI or not.
Joined: Sep 26, 2012
OK, I've got to go back to the drawing board here. I'm going to go read some of my book(s) I have and see if I can come up with something. I'll check back here afterward. Thanks everyone for the help so far...
Dan D'Apice wrote:Kemal,
What I want to do is just populate the text fields of the GUI with their respected data. E.G. itemNumber would be placed in the Item Number text field, itemName would go in the Item Name text field, etc. ...
Since you keep your entire inventory in a collection (it contains all of the books) how do you choose of which one the details will be displayed? I don't really get what you mean...
But if you want to keep the things simple for start, try this. For simplicity, create just one instance of your Book class (forget the collection for now). When you do that, you'll populate corresponding fields with the following:
This is the example in case you have only one book to display.
In the case of entire inventory, I wrote that in my previous post. You would have a field in your GUI class that would be some collection:
Now, when you want to display details of a specific book, all you need to do is get it from the inventory collection and pass it as an argument to displayDetails() method shown above. How you're going to let the user choose the book to display is up to you; one approach is the one you had in the original post, using JComboBox with the list of all books from your inventory.
Joined: Sep 26, 2012
Sorry I didn't respond earlier, I had some personal things to take care of yesterday. Anyway, thanks for that last posting. I'm going to give that a shot and see how it works out. Wish me luck....lol...I'm gunna need it....
subject: Making a GUI run and displaying data from a program that is already created.