Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I am totally confused how to modify my Inverntory Program Part 3 to add GUI

  1. #1
    Junior Member
    Join Date
    Apr 2011
    My Mood
    Thanked 0 Times in 0 Posts

    Default I am totally confused how to modify my Inverntory Program Part 3 to add GUI

    This is the basic GUI I have coded so far but not sure how to incorporated into my existing code. Need help deseperately already a day late turning this in.

    import java.awt.*;  
      import javax.swing.*;  
      import java.awt.event.*;  
      public class RectangleProgram extends JFrame  
          private static final int WIDTH = 400;  
          private static final int HEIGHT = 300;  
          private JLabel lengthL, widthL, areaL;  
          private JTextField lengthTF, widthTF, areaTF;  
          private JButton calculateB, exitB;  
          //Button handlers:  
         private CalculateButtonHandler cbHandler;  
         private ExitButtonHandler ebHandler;  
          public RectangleProgram()  
              lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);  
              widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);  
              areaL = new JLabel("Area: ", SwingConstants.RIGHT);  
              lengthTF = new JTextField(10);  
              widthTF = new JTextField(10);  
              areaTF = new JTextField(10);  
              //SPecify handlers for each button and add (register) ActionListeners to each button.  
              calculateB = new JButton("Calculate");  
              cbHandler = new CalculateButtonHandler();  
              exitB = new JButton("Exit");  
              ebHandler = new ExitButtonHandler();  
              setTitle("Sample Title: Area of a Rectangle");  
              Container pane = getContentPane();  
              pane.setLayout(new GridLayout(4, 2));  
              //Add things to the pane in the order you want them to appear (left to right, top to bottom)  
              setSize(WIDTH, HEIGHT);  
          private class CalculateButtonHandler implements ActionListener  
             public void actionPerformed(ActionEvent e)  
                  double width, length, area;  
                  length = Double.parseDouble(lengthTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.  
                  width = Double.parseDouble(widthTF.getText());  
                  area = length * width;  
                  areaTF.setText("" + area);  
         public class ExitButtonHandler implements ActionListener  
             public void actionPerformed(ActionEvent e)  
          public static void main(String[] args)  
              RectangleProgram rectObj = new RectangleProgram();  

    //W6 CheckPoint: InventoryPart3.java
    //Inventory Program Part 3 Week 6 class Television modified with additional class and restocking fee
    //import statements
    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Arrays;
      class Television { //class name and attributes
        //declare class variables
     	private String itemNumber; //item # of product
     	private String televisionName; //product name
     	public int unitsinStock; //# of units in stock
     	public double unitPrice; //Price per unit
      	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
     	//class constructor
     	public Television(String itemNumber, String televisionName, int unitsinStock, double price) {
     		this.itemNumber = itemNumber;
     		this.televisionName = televisionName;
     		this.unitsinStock = unitsinStock;
     		this.unitPrice = price;
     } //end constructor
      //define set and get methods for class variables
     //getter and setter methods for Television
     //item number
        public String getItemNumber() { //getter for item number
     		return itemNumber;
     	} //end getter item number
      	public void setItemNumber (String itemNumber) { //setter for item number
     		this.itemNumber = itemNumber;
     	} //end setter item number
     	//television name
        public String getTelevisionName() { //getter for product name
    	 		return televisionName;
     	} //end getter television name
     	public void setTelevisionName (String product) { //setter for product name
     		this.televisionName = televisionName;
     	} //end setter television name
         //available units
        public double getUnitsInStock() { //getter for units in stock
    	  		return unitsinStock;
    	} //end getter units in stock
     	public void setUnitsInStock (double units) { //setter for units in stock
     		this.unitsinStock = unitsinStock;
     	} //end setter units in stock
        public double getUnitPrice() { //getter for unit price
    	 		return unitPrice;
     	} //end getter for unit price
     	public void setUnitPrice (double price) { //setter for unit price
     		this.unitPrice = unitPrice;
     	} //end setter unit price
         //calculate the total inventory by returning the product of available units and price
    	public double calculateInventory(){
    		return unitPrice * unitsinStock;
        //toString method that outputs the class variables
    	public String toString () {
    			return "Television Name:" + "\t" + televisionName + "\n" +
    				   "Item Number:" + "\t" + itemNumber + "\n" +
    			       "UnitsInStock:" + "\t \t" + unitsinStock + "\n" +
    			       "UnitPrice:" + "\t \t" + nf.format(unitPrice) + "\n" +
    			       "Item Total:" + "\t" + nf.format(calculateInventory());
    // The is-a test the inheritance relationship between Samsung and Television
    class Televisions extends Television{
    	//class variables
    	private String serialNumber;
    	private double restockFee;
    	private static final double RESTOCK_FEE_PERCENTAGE = .05;
    	//class constructor
    	public Televisions(String itemNumber, String televisionName, int unitsinStock, double price, String serialNumber){
    		super (itemNumber, televisionName, unitsinStock, price);
    		this.serialNumber = serialNumber;
    		//calculate restock fee as 5% of the inventory total
    		restockFee = super.calculateInventory() * RESTOCK_FEE_PERCENTAGE;
    	//declare get and set methods
    	//serial number
    	public String getSerialNumber(){
    		return serialNumber;
    	public void setSerialNumber(String serialNumber){
    		this.serialNumber = serialNumber;
    	//restockFee does not have a set method, to ensure that it is only set through the HP constructor
    	public double getRestockFee(){
    		return restockFee;
    	//add restock fee and serial number to the output
    	public String toString () {
    				return super.toString() + "\n" +
    				       "Serial Number: " + "\t" + serialNumber + "\n" +
    				       "Restock Fee:" + "\t" + nf.format(getRestockFee());
    public class InventoryPart3 {
        public static Television[] sortArray(Television[] televisions){
            // First Step
            String[] names = new String[televisions.length];
           //Second Step
           Television[] tempProduct = new Television [televisions.length];
          //Third Step
          for (int i = 0; i < televisions.length; i++){
    	  		 names[i] = televisions[i].getTelevisionName();
          //Fourth Step
         //Fifth Step
        	for (int i = 0; i < televisions.length; i++){ //Product Array
    			     for (int j = 0; j < names.length; j++){ //Names Array
    			          if (televisions[i].getTelevisionName().equalsIgnoreCase(names[j])) {
    			                     tempProduct[j] = televisions[i];
    			return tempProduct;
     	public static double calculateInventoryTotal(Television[] televisions){
    		double total = 0;
    		for (int i = 0; i < televisions.length; i++){
    			total += televisions[i].calculateInventory();
    		return total;
        public static void main (String[] args) {
            NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
           //create several items for the inventory of type Television and LG
           Television[] televisions = new Television[5];
           Television samsung = new Television ("003", "Samsung UN46D6400",9,1599.99);
           Television vizio = new Television ("005", "Vizio XVT553SV",6,1299.00);
           Television panasonic = new Television ("002", "Panasonic Viera TC-P50VT25",2,2079.99);
           Television sony = new Television ("004", "Sony Bravia KDL-55EX720",8, 1889.99);
           Television lg = new Television ("001", "LG Infinia 47LX9500",2,2099.00);
    //add all of the items to the inventory
    		televisions[2] = samsung;
    		televisions[4] = vizio;
    		televisions[1] = panasonic;
    		televisions[3] = sony;
    		televisions[0] = lg;
            //sort items
    		televisions = sortArray(televisions);
    		//calculate inventory total
    		double inventoryTotal = calculateInventoryTotal(televisions);
    		//output inventory
    		System.out.println("Inventory Items: " + "\n");
    		for (int i = 0; i < televisions.length; i++){
    		//output inventory total
    		System.out.println("Inventory Total: " + nf.format(inventoryTotal));
    Last edited by Raven9; April 25th, 2011 at 11:34 PM. Reason: Here is my Inventory Program Part 3 that I need to add the GUI to.

  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 779 Times in 725 Posts
    Blog Entries

    Default Re: I am totally confused how to modify my Inverntory Program Part 3 to add GUI

    To get good help faster, I recommend you ask a more specific question. How To Ask Questions The Smart Way

  3. The Following User Says Thank You to copeg For This Useful Post:

    Raven9 (April 26th, 2011)

Similar Threads

  1. How to modify System.out.println()
    By emailkia in forum Java Theory & Questions
    Replies: 6
    Last Post: April 25th, 2011, 12:40 AM
  2. File I/O Modify Text Problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 26th, 2010, 04:30 AM
  3. Totally stuck on this assignment
    By Sgiahatch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2010, 04:25 PM
  4. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM
  5. how to modify a JList Frame?
    By JM_4ever in forum AWT / Java Swing
    Replies: 0
    Last Post: October 14th, 2009, 11:58 PM

Tags for this Thread