473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

GUI Headache

Hello,

I'm currently working on the dreaded Inventory Program. I'm on part four and just learning GUIs. I need to modify the Inventory Program to use a GUI. In the GUI, it should display the information in my array one line at a time as well as the value of my entire inventory, additional feature and restocking fee. I was able to create the GUI, but I can't populate it with my array items. Any tips on getting my array information to display on the GUI would be wonderful! I am able to compile the file, but get an empty GUI.

Any assistance is greatly appreciated!!!!!

Here is my array code and the dispaly call to the GUI in my Main class:
Expand|Select|Wrap|Line Numbers
  1. // constructor
  2.     public Inventory_Program4()
  3.     {        
  4.         double totalInventoryValue = 0.0;
  5.  
  6.         // Specify how many items are in the array
  7.         // instantiate Book2 object
  8.         myBook = new Book2[5];
  9.         myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
  10.         myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
  11.         myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
  12.         myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
  13.         myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
  14.  
  15.         // call method sort book inventory for display
  16.         sortBookInventory();
  17.  
  18.         for (int i = 0; i<5; i++)
  19.         {
  20.  
  21.             // display the book title
  22.             System.out.println("The book title is: " + myBook[i].getBookTitle());
  23.  
  24.             // display the item number
  25.             System.out.println("The item number is: " + myBook[i].getItemNumber());
  26.  
  27.             // display the number of units in stock
  28.             System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
  29.  
  30.             // display the price per book
  31.             System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
  32.  
  33.             // display the value of the inventory
  34.             System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
  35.  
  36.             // display the book author
  37.             System.out.println("The book author is: " + myBook[i].getBookAuthor());
  38.  
  39.             // display the restocking fee
  40.             System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);
  41.  
  42.             // calculate total inventory value with restocking fee added
  43.             totalInventoryValue += (myBook[i].inventoryValue() * 1.05);
  44.  
  45.             // insert blank line
  46.             System.out.println();
  47.  
  48.  
  49.         } // end for
  50.  
  51.         // display the total value of the inventory with the restocking fee
  52.         System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n", totalInventoryValue);
  53.  
  54.         // display the total value of the inventory excluding the restocking fee
  55.         System.out.println("The total value of the book inventory is: " + totalInventoryValue());
  56.  
  57.             // calling GUI to display contents
  58.             Labels labels = new Labels(); // create Labels
  59.             labels.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  60.             labels.setSize(275, 180); // set frame size
  61.             labels.setVisible( true ); // display frame
  62.  
  63.     } // end constructor
Here is my the code to my Labels class where I defined all the JLabels for the GUI:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.FlowLayout;
  2. import javax.swing.JFrame;
  3. import javax.swing.JLabel;
  4. import javax.swing.SwingConstants;
  5. import javax.swing.Icon;
  6. import javax.swing.ImageIcon;
  7.  
  8. public class Labels extends JFrame
  9. {
  10.  
  11.     private JLabel bookTitleLabel;
  12.     private JLabel itemNumberLabel;
  13.     private JLabel bookUnitsLabel;
  14.     private JLabel bookPriceLabel;
  15.     private JLabel inventoryValueLabel;
  16.     private JLabel bookAuthorLabel;
  17.     private JLabel bookRestockingFeeLabel;
  18.     private JLabel totalInventoryValueLabel;
  19.  
  20.     //  Labels constructor adds JLabels to JFrame
  21.     public Labels()
  22.     {
  23.     super( "Inventory Program" );
  24.     setLayout( new FlowLayout() ); // set frame layout
  25.  
  26.     // JLabel constructor
  27.     bookTitleLabel = new JLabel("Book Title:");
  28.     bookTitleLabel.setToolTipText("Book Title Label");
  29.     add( bookTitleLabel ); // add bookTitleLabel to JFrame
  30.  
  31.     itemNumberLabel = new JLabel("Item Number:");
  32.     itemNumberLabel.setToolTipText("Item Number Label");
  33.     add( itemNumberLabel ); // add itemNumberLabel to JFrame
  34.  
  35.     bookUnitsLabel = new JLabel("Book Units:");
  36.     bookUnitsLabel.setToolTipText("Book Units Label");
  37.     add( bookUnitsLabel ); // add bookUnitsLabel to JFrame
  38.  
  39.     bookPriceLabel = new JLabel("Book Price:");
  40.     bookPriceLabel.setToolTipText("Book Price Label");
  41.     add( bookPriceLabel ); // add bookPriceLabel to JFrame
  42.  
  43.     inventoryValueLabel = new JLabel("Book Inventory Value:");
  44.     inventoryValueLabel.setToolTipText("Inventory Value Label");
  45.     add ( inventoryValueLabel ); // add inventoryValueLabel to JFrame
  46.  
  47.     bookAuthorLabel = new JLabel("Book Author:");
  48.     bookAuthorLabel.setToolTipText("Book Author Label");
  49.     add( bookAuthorLabel ); // add bookAuthorLabel to JFrame
  50.  
  51.     bookRestockingFeeLabel = new JLabel("Restocking Fee:");
  52.     bookRestockingFeeLabel.setToolTipText("Restocking Fee Label");
  53.     add( bookRestockingFeeLabel ); // add bookRestockingFeeLabel to JFrame
  54.  
  55.     totalInventoryValueLabel = new JLabel("Total Inventory Value:");
  56.     totalInventoryValueLabel.setToolTipText("Total Inventory Label");
  57.     add( totalInventoryValueLabel ); // add totalInventoryValueLabel to JFrame
  58.     } // end Label constructor
  59. } // end class Labels
Sep 25 '07 #1
7 1750
Nepomuk
3,112 Expert 2GB
Hello,

I'm currently working on the dreaded Inventory Program. I'm on part four and just learning GUIs. I need to modify the Inventory Program to use a GUI. In the GUI, it should display the information in my array one line at a time as well as the value of my entire inventory, additional feature and restocking fee. I was able to create the GUI, but I can't populate it with my array items. Any tips on getting my array information to display on the GUI would be wonderful! I am able to compile the file, but get an empty GUI.

Any assistance is greatly appreciated!!!!!

Here is my array code and the dispaly call to the GUI in my Main class:
...
Hi!
I would add a setItem(...) method to the GUI, which set's the contents of those JLabels. It can get it's information from your Inventory and write it into the Labels with setText("Blabla: " + valueToBeSet).

Otherwise, you'll just have to keep on trying.

Greetings,
Nepomuk
Sep 25 '07 #2
Thank you! I will try that and let you know!
Sep 25 '07 #3
I've been playing around with this for hours and I'm still stuck. Can you post an example of how to add a setItem method to the GUI?

Thanks again!
Sep 25 '07 #4
r035198x
13,262 8TB
I've been playing around with this for hours and I'm still stuck. Can you post an example of how to add a setItem method to the GUI?

Thanks again!
Well then what code do you have now and where are you stuck with it?
Sep 25 '07 #5
JosAH
11,448 Expert 8TB
I've been playing around with this for hours and I'm still stuck. Can you post an example of how to add a setItem method to the GUI?

Thanks again!
Please don't beg for code; if you read the API documentatio you'd find lots and
lots of code examples and explanations. Try to think in detail about the problem
you're supposed to be working on. It's the only way to slaughter the problem and
master the Java language.

kind regards,

Jos
Sep 25 '07 #6
Here is my current code. I am trying to get my array items to display in the GUI. The file compiles, but I get the following error message when the GUI tries to lauch.

Exception in thread "main" java.lang.NullPointerException
at Labels.<init>(Labels.java:36)
at Inventory_Program4.<init>(Inventory_Program4.java: 98)
at Inventory_Program4.main(Inventory_Program4.java:12 1)

I know that the following line is giving me the grief (bookTitleLabel.setText("The Book Title is: " + myBook[i].getBookTitle());). I'm trying to display the array item, but not doing it correctly.

I am so lost on how to incorporate my array information into the GUI. I apologize if I sound like I'm begging for code, that was not my intention. Thanks for all of your help!

The following code is where I created my GUI:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.FlowLayout;
  2. import javax.swing.JFrame;
  3. import javax.swing.JLabel;
  4. import javax.swing.SwingConstants;
  5. import javax.swing.Icon;
  6. import javax.swing.ImageIcon;
  7.  
  8. public class Labels extends JFrame
  9. {
  10.  
  11. private Book2 myBook[];
  12.  
  13.  
  14.     private JLabel bookTitleLabel;
  15.     private JLabel itemNumberLabel;
  16.     private JLabel bookUnitsLabel;
  17.     private JLabel bookPriceLabel;
  18.     private JLabel inventoryValueLabel;
  19.     private JLabel bookAuthorLabel;
  20.     private JLabel bookRestockingFeeLabel;
  21.     private JLabel totalInventoryValueLabel;
  22.  
  23.     //  Labels constructor adds JLabels to JFrame
  24.     public Labels()
  25.     {
  26.     super( "Inventory Program" );
  27.     setLayout( new FlowLayout() ); // set frame layout
  28.  
  29.  
  30.  
  31.     for (int i = 0; i<5; i++)
  32.     {
  33.  
  34.     // JLabel constructor
  35.     bookTitleLabel = new JLabel();
  36.     bookTitleLabel.setText("The Book Title is: " + myBook[i].getBookTitle());
  37.     bookTitleLabel.setToolTipText("Book Title Label");
  38.     add( bookTitleLabel ); // add bookTitleLabel to JFrame
  39.  
  40.     itemNumberLabel = new JLabel("Item Number:");
  41.     itemNumberLabel.setToolTipText("Item Number Label");
  42.     add( itemNumberLabel ); // add itemNumberLabel to JFrame
  43.  
  44.     bookUnitsLabel = new JLabel("Book Units:");
  45.     bookUnitsLabel.setToolTipText("Book Units Label");
  46.     add( bookUnitsLabel ); // add bookUnitsLabel to JFrame
  47.  
  48.     bookPriceLabel = new JLabel("Book Price:");
  49.     bookPriceLabel.setToolTipText("Book Price Label");
  50.     add( bookPriceLabel ); // add bookPriceLabel to JFrame
  51.  
  52.     inventoryValueLabel = new JLabel("Book Inventory Value:");
  53.     inventoryValueLabel.setToolTipText("Inventory Value Label");
  54.     add ( inventoryValueLabel ); // add inventoryValueLabel to JFrame
  55.  
  56.     bookAuthorLabel = new JLabel("Book Author:");
  57.     bookAuthorLabel.setToolTipText("Book Author Label");
  58.     add( bookAuthorLabel ); // add bookAuthorLabel to JFrame
  59.  
  60.     bookRestockingFeeLabel = new JLabel("Restocking Fee:");
  61.     bookRestockingFeeLabel.setToolTipText("Restocking Fee Label");
  62.     add( bookRestockingFeeLabel ); // add bookRestockingFeeLabel to JFrame
  63.  
  64.     totalInventoryValueLabel = new JLabel("Total Inventory Value:");
  65.     totalInventoryValueLabel.setToolTipText("Total Inventory Label");
  66.     add( totalInventoryValueLabel ); // add totalInventoryValueLabel to JFrame
  67. }
  68.     } // end Label constructor
  69. } // end class Labels
The following code is my array in my Main class:
Expand|Select|Wrap|Line Numbers
  1. // constructor
  2.     public Inventory_Program4()
  3.     {        
  4.         double totalInventoryValue = 0.0;
  5.  
  6.         // Specify how many items are in the array
  7.         // instantiate Book2 object
  8.         myBook = new Book2[5];
  9.         myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
  10.         myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
  11.         myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
  12.         myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
  13.         myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
  14.  
  15.         // call method sort book inventory for display
  16.         sortBookInventory();
  17.  
  18.         for (int i = 0; i<5; i++)
  19.         {
  20.  
  21.             // display the book title
  22.             System.out.println("The book title is: " + myBook[i].getBookTitle());
  23.  
  24.             // display the item number
  25.             System.out.println("The item number is: " + myBook[i].getItemNumber());
  26.  
  27.             // display the number of units in stock
  28.             System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
  29.  
  30.             // display the price per book
  31.             System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
  32.  
  33.             // display the value of the inventory
  34.             System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
  35.  
  36.             // display the book author
  37.             System.out.println("The book author is: " + myBook[i].getBookAuthor());
  38.  
  39.             // display the restocking fee
  40.             System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);
  41.  
  42.             // calculate total inventory value with restocking fee added
  43.             totalInventoryValue += (myBook[i].inventoryValue() * 1.05);
  44.  
  45.             // insert blank line
  46.             System.out.println();
  47.  
  48.  
  49.         } // end for
  50.  
  51.         // display the total value of the inventory with the restocking fee
  52.         System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n", totalInventoryValue);
  53.  
  54.         // display the total value of the inventory excluding the restocking fee
  55.         System.out.println("The total value of the book inventory is: " + totalInventoryValue());
  56.  
  57.             // calling GUI to display contents
  58.             Labels labels = new Labels(); // create Labels
  59.             labels.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  60.             labels.setSize(275, 180); // set frame size
  61.             labels.setVisible( true ); // display frame
  62.  
  63.     } // end constructor
Sep 25 '07 #7
r035198x
13,262 8TB
private Book2 myBook[];
Does not create an array at all.
myBook at this point is null. So when the code reaches that point in that Labels constructor and tries to do myBook[i].getWhatever it tells you that you are derefencing a null variable. The exception trace does tell you what is wrong as you can see.
Sep 25 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: David | last post by:
On every web browser except Safari, this website works great. (Well, by "every" I mean Mozilla, Netscape, and Internet Explorer, for Mac and Windows). The site is: http://www.ruleofthirds.com ...
5
by: Ryan Ternier | last post by:
I'm having an issue with an SQL insert statement. It's a very simple statement, and it's causing too much fuss. strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID, OrderID, Hidden) VALUES("...
3
by: laurenq uantrell | last post by:
I'm trying to return an integer from the following table that returns the number of unique cities: tblEmployees Name City John Boston Frank New York Jim Omaha Betty ...
12
by: NuB | last post by:
The validation controls are giving me a headache. here is what i'm trying to do and so far what I've tried has not worked. I need to hide my grid if the page is not valid how can i accomplish...
5
by: James Ma | last post by:
Now I am debugging a buggy VB.NET function like following Function foo() Try ‘ many loops and codes and nested try catch here! …… Catch (ex as Exception) Msgbox ex.message Finally …
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.