GUI Headache

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pixiedustjt
    New Member
    • Sep 2007
    • 7

    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:
    Code:
    // constructor
    	public Inventory_Program4()
    	{		
    		double totalInventoryValue = 0.0;
    
    		// Specify how many items are in the array
    		// instantiate Book2 object
    		myBook = new Book2[5];
    		myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
    		myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
    		myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
    		myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
    		myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
    			
    		// call method sort book inventory for display
    		sortBookInventory();
    
    		for (int i = 0; i<5; i++)
    		{
    
    			// display the book title
    			System.out.println("The book title is: " + myBook[i].getBookTitle());
    
    			// display the item number
    			System.out.println("The item number is: " + myBook[i].getItemNumber());
    
    			// display the number of units in stock
    			System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
    
    			// display the price per book
    			System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
    
    			// display the value of the inventory
    			System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
    
    			// display the book author
    			System.out.println("The book author is: " + myBook[i].getBookAuthor());
    
    			// display the restocking fee
    			System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);
    
    			// calculate total inventory value with restocking fee added
    			totalInventoryValue += (myBook[i].inventoryValue() * 1.05);
    
    			// insert blank line
    			System.out.println();
    
    
    		} // end for
    
    		// display the total value of the inventory with the restocking fee
    		System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n", totalInventoryValue);
    
    		// display the total value of the inventory excluding the restocking fee
    		System.out.println("The total value of the book inventory is: " + totalInventoryValue());
    
    			// calling GUI to display contents
    			Labels labels = new Labels(); // create Labels
    			labels.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    			labels.setSize(275, 180); // set frame size
    			labels.setVisible( true ); // display frame
    
    	} // end constructor
    Here is my the code to my Labels class where I defined all the JLabels for the GUI:
    Code:
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    
    public class Labels extends JFrame
    {
    
    	private JLabel bookTitleLabel;
    	private JLabel itemNumberLabel;
    	private JLabel bookUnitsLabel;
    	private JLabel bookPriceLabel;
    	private JLabel inventoryValueLabel;
    	private JLabel bookAuthorLabel;
    	private JLabel bookRestockingFeeLabel;
    	private JLabel totalInventoryValueLabel;
    
    	//  Labels constructor adds JLabels to JFrame
    	public Labels()
    	{
    	super( "Inventory Program" );
    	setLayout( new FlowLayout() ); // set frame layout
    
    	// JLabel constructor
    	bookTitleLabel = new JLabel("Book Title:");
    	bookTitleLabel.setToolTipText("Book Title Label");
    	add( bookTitleLabel ); // add bookTitleLabel to JFrame
    
    	itemNumberLabel = new JLabel("Item Number:");
    	itemNumberLabel.setToolTipText("Item Number Label");
    	add( itemNumberLabel ); // add itemNumberLabel to JFrame
    
    	bookUnitsLabel = new JLabel("Book Units:");
    	bookUnitsLabel.setToolTipText("Book Units Label");
    	add( bookUnitsLabel ); // add bookUnitsLabel to JFrame
    
    	bookPriceLabel = new JLabel("Book Price:");
    	bookPriceLabel.setToolTipText("Book Price Label");
    	add( bookPriceLabel ); // add bookPriceLabel to JFrame
    	
    	inventoryValueLabel = new JLabel("Book Inventory Value:");
    	inventoryValueLabel.setToolTipText("Inventory Value Label");
    	add ( inventoryValueLabel ); // add inventoryValueLabel to JFrame
    
    	bookAuthorLabel = new JLabel("Book Author:");
    	bookAuthorLabel.setToolTipText("Book Author Label");
    	add( bookAuthorLabel ); // add bookAuthorLabel to JFrame
    	
    	bookRestockingFeeLabel = new JLabel("Restocking Fee:");
    	bookRestockingFeeLabel.setToolTipText("Restocking Fee Label");
    	add( bookRestockingFeeLabel ); // add bookRestockingFeeLabel to JFrame
    
    	totalInventoryValueLabel = new JLabel("Total Inventory Value:");
    	totalInventoryValueLabel.setToolTipText("Total Inventory Label");
    	add( totalInventoryValueLabel ); // add totalInventoryValueLabel to JFrame
    	} // end Label constructor
    } // end class Labels
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by pixiedustjt
    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

    Comment

    • pixiedustjt
      New Member
      • Sep 2007
      • 7

      #3
      Thank you! I will try that and let you know!

      Comment

      • pixiedustjt
        New Member
        • Sep 2007
        • 7

        #4
        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!

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by pixiedustjt
          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?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by pixiedustjt
            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

            Comment

            • pixiedustjt
              New Member
              • Sep 2007
              • 7

              #7
              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.NullP ointerException
              at Labels.<init>(L abels.java:36)
              at Inventory_Progr am4.<init>(Inve ntory_Program4. java:98)
              at Inventory_Progr am4.main(Invent ory_Program4.ja va:121)

              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:
              Code:
              import java.awt.FlowLayout;
              import javax.swing.JFrame;
              import javax.swing.JLabel;
              import javax.swing.SwingConstants;
              import javax.swing.Icon;
              import javax.swing.ImageIcon;
              
              public class Labels extends JFrame
              {
              	
              private Book2 myBook[];
              
              
              	private JLabel bookTitleLabel;
              	private JLabel itemNumberLabel;
              	private JLabel bookUnitsLabel;
              	private JLabel bookPriceLabel;
              	private JLabel inventoryValueLabel;
              	private JLabel bookAuthorLabel;
              	private JLabel bookRestockingFeeLabel;
              	private JLabel totalInventoryValueLabel;
              
              	//  Labels constructor adds JLabels to JFrame
              	public Labels()
              	{
              	super( "Inventory Program" );
              	setLayout( new FlowLayout() ); // set frame layout
              
              
              	
              	for (int i = 0; i<5; i++)
              	{
              	
              	// JLabel constructor
              	bookTitleLabel = new JLabel();
              	bookTitleLabel.setText("The Book Title is: " + myBook[i].getBookTitle());
              	bookTitleLabel.setToolTipText("Book Title Label");
              	add( bookTitleLabel ); // add bookTitleLabel to JFrame
              
              	itemNumberLabel = new JLabel("Item Number:");
              	itemNumberLabel.setToolTipText("Item Number Label");
              	add( itemNumberLabel ); // add itemNumberLabel to JFrame
              
              	bookUnitsLabel = new JLabel("Book Units:");
              	bookUnitsLabel.setToolTipText("Book Units Label");
              	add( bookUnitsLabel ); // add bookUnitsLabel to JFrame
              
              	bookPriceLabel = new JLabel("Book Price:");
              	bookPriceLabel.setToolTipText("Book Price Label");
              	add( bookPriceLabel ); // add bookPriceLabel to JFrame
              	
              	inventoryValueLabel = new JLabel("Book Inventory Value:");
              	inventoryValueLabel.setToolTipText("Inventory Value Label");
              	add ( inventoryValueLabel ); // add inventoryValueLabel to JFrame
              
              	bookAuthorLabel = new JLabel("Book Author:");
              	bookAuthorLabel.setToolTipText("Book Author Label");
              	add( bookAuthorLabel ); // add bookAuthorLabel to JFrame
              	
              	bookRestockingFeeLabel = new JLabel("Restocking Fee:");
              	bookRestockingFeeLabel.setToolTipText("Restocking Fee Label");
              	add( bookRestockingFeeLabel ); // add bookRestockingFeeLabel to JFrame
              
              	totalInventoryValueLabel = new JLabel("Total Inventory Value:");
              	totalInventoryValueLabel.setToolTipText("Total Inventory Label");
              	add( totalInventoryValueLabel ); // add totalInventoryValueLabel to JFrame
              }
              	} // end Label constructor
              } // end class Labels
              The following code is my array in my Main class:
              Code:
              // constructor
              	public Inventory_Program4()
              	{		
              		double totalInventoryValue = 0.0;
              
              		// Specify how many items are in the array
              		// instantiate Book2 object
              		myBook = new Book2[5];
              		myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
              		myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
              		myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
              		myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
              		myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
              			
              		// call method sort book inventory for display
              		sortBookInventory();
              
              		for (int i = 0; i<5; i++)
              		{
              
              			// display the book title
              			System.out.println("The book title is: " + myBook[i].getBookTitle());
              
              			// display the item number
              			System.out.println("The item number is: " + myBook[i].getItemNumber());
              
              			// display the number of units in stock
              			System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
              
              			// display the price per book
              			System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
              
              			// display the value of the inventory
              			System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
              
              			// display the book author
              			System.out.println("The book author is: " + myBook[i].getBookAuthor());
              
              			// display the restocking fee
              			System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);
              
              			// calculate total inventory value with restocking fee added
              			totalInventoryValue += (myBook[i].inventoryValue() * 1.05);
              
              			// insert blank line
              			System.out.println();
              
              
              		} // end for
              
              		// display the total value of the inventory with the restocking fee
              		System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n", totalInventoryValue);
              
              		// display the total value of the inventory excluding the restocking fee
              		System.out.println("The total value of the book inventory is: " + totalInventoryValue());
              
              			// calling GUI to display contents
              			Labels labels = new Labels(); // create Labels
              			labels.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              			labels.setSize(275, 180); // set frame size
              			labels.setVisible( true ); // display frame
              
              	} // end constructor

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                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.

                Comment

                Working...