8 Replies - 293 Views - Last Post: 11 March 2012 - 01:01 PM Rate Topic: -----

#1 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Trouble populating my GUI window

Posted 11 March 2012 - 11:50 AM

OK guys I am completely lost I Managed to create the GUI window for my inventory program but I cannot figure out how to add the inventory program output to the window. I have taken several tutorials, read through my text book, and look at many different examples but I am just not understanding it. Can someone either refer me to or provide a good example that will explain what I need to do or maybe walk me through it.
Here is my code:
Monitor class:
import java.util.Comparator;
import java.text.NumberFormat;
import java.util.Locale;

public class Monitor implements Comparable<Monitor>
{
     private String prodNumber; // product number
     private String prodName; // product name
     private int unitsTotal; // total units in stock
     private double unitPrice; // price per unit
     NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);


     // initialize four-argument constructor
     public Monitor( String prodNumber, String prodName, int unitsTotal, double unitPrice)
     {

          this.prodNumber = prodNumber;
          this.prodName = prodName;
          this.unitsTotal = unitsTotal; // validate and store Monitor total
          this.unitPrice = unitPrice; // validate and store monitor price

      } // end four-argument constructor


	// Set and get methods
     public void setProdNumber( String prodNumber )
     {
          this.prodNumber = prodNumber;
     }
     public String getProdNumber()
     {
          return prodNumber;
     }
     public void setProdName( String prodName)
     {
          this.prodName = prodName;
     }
     public String getProdName()
     {
          return prodName;
     }
     public void setUnitsTotal( int unitsTotal )
     {
          this.unitsTotal = unitsTotal;
     }
     public int getUnitsTotal()
     {
          return unitsTotal;
     }
     public void setUnitPrice( double unitPrice )
     {
          this.unitPrice = unitPrice;
     }
     public double getUnitPrice()
     {
           return unitPrice;
     }
     public double getValue()
     {
          return unitsTotal * unitPrice;
     }


      //toString method
	 public String toString ()
	 {
			return "Printer Name:" + "\t" + prodName + "\n" +
				   "Item Number:" + "\t" + prodNumber + "\n" +
			       "Units:" + "\t \t" + unitsTotal + "\n" +
			       "Price:" + "\t \t" + nf.format(unitPrice) + "\n" +
			       "Item Total:" + "\t" + nf.format(getValue());
     }


     public int compareTo(Monitor compareMonitor)
     {
     int compareTotal = ((Monitor) compareMonitor).getUnitsTotal();

	 return this.unitsTotal - compareTotal;
	 }

     public static Comparator<Monitor> MonitorNameComparator = new Comparator<Monitor>()
     {
	 public int compare(Monitor monitor1, Monitor monitor2)
	 {
	 String monitorName1 = monitor1.getProdName().toUpperCase();
	 String monitorName2 = monitor2.getProdName().toUpperCase();

	 return monitorName1.compareTo(monitorName2);
     }
   };

 } // end class Monitors



Monitor3D class
public class Monitor3D extends Monitor
{

    private String serialNumber;
    private double restockingFee;
    private static final double restockFeePercent = .05;

public Monitor3D( String prodNumber, String prodName, int unitsTotal, double unitPrice, String serialNumber)
     {
		 super(prodNumber, prodName, unitsTotal, unitPrice);
		 this.serialNumber = serialNumber;
		 restockingFee = unitPrice * restockFeePercent;
	 }

public void setSerialNumber(String serialNumber)
{
		this.serialNumber = serialNumber;
}

public String getSerialNumber()
{
		return serialNumber;
}

public double getRestockingFee()
{
		return restockingFee;
}

public String toString ()
{
		return super.toString() + "\n" + "Serial Number: " + "\t" + serialNumber + "\n" + "Restock Fee:" + "\t" + nf.format(getRestockingFee());
}


}



MonitorGUI class
import javax.swing.*;
import java.awt.*;

public class MonitorGUI extends JFrame{


	JTextArea ta = new JTextArea(25,40);


	JScrollPane sp = new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

	JPanel pane = new JPanel();

	public MonitorGUI() {
		super("Monitor Inventory Progrom");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(100,100,500,500);
		pane.add(sp);
		add(pane);
		setVisible(true);
	}

}


MonitorInventory class
import java.util.Comparator;
import java.awt.Window;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;


public class  MonitorInventory
{

 static MonitorGUI mainGUI = new MonitorGUI();
 public static void main(String args[]) {
	

		 NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

		 Monitor [] monitors = new Monitor[5];

          Monitor B = new Monitor("2479","Monitor B", 20, 450.00);
          Monitor A = new Monitor("2480", "Monitor A", 10, 350.00);
          Monitor C = new Monitor("2481", "Monitor C", 15, 250.00);
          Monitor3D E = new Monitor3D("2482", "Monitor3D E", 10, 150.55, "25947");
          Monitor3D D = new Monitor3D("2483", "Monitor3D D", 12, 250.25, "11234");{

          monitors[0] = A;
          monitors[1] = B;
          monitors[2] = C;
          monitors[3] = E;
          monitors[4] = D;

          Arrays.sort(monitors, Monitor.MonitorNameComparator);

          System.out.println("Monitor Inventory Progrom");
          System.out.println();

              // get monitor information
          for(int i1 = 0; i1 < 5; i1++)

          {
          System.out.println(String.format("Product Name:    %s\nProduct ID:      %s\nTotal Units:     %d\nUnit Price:      $%.2f\nTotal Value:     $%.2f\n", monitors[i1].getProdName(), monitors[i1].getProdNumber(), monitors[i1].getUnitsTotal(), monitors[i1].getUnitPrice (), monitors[i1].getValue()));

    if(monitors[i1] instanceof Monitor3D)
{
Monitor3D m3D = (Monitor3D)monitors[i1];
System.out.println(String.format("Serial Number:   %s", m3D.getSerialNumber()));
System.out.printf("Restocking Fee:  $%.2f\n\n", m3D .getRestockingFee());
}

}


   double value = 0.0;
   for (int i = 0;i < monitors.length; i++)
   {
	   value = value + monitors[i].getValue();

  }
System.out.println("Inventory Value: $" + value);



          }
 }
} //


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble populating my GUI window

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,174
  • Joined: 20-September 08

Re: Trouble populating my GUI window

Posted 11 March 2012 - 12:08 PM

Before going any further, you need to realize that the number of units does not belong to the entity itself. It actually belongs to some sort of inventory class
Was This Post Helpful? 1
  • +
  • -

#3 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble populating my GUI window

Posted 11 March 2012 - 12:15 PM

View Postg00se, on 11 March 2012 - 12:08 PM, said:

Before going any further, you need to realize that the number of units does not belong to the entity itself. It actually belongs to some sort of inventory class

Not sure I follow you here. I believe it belongs to my MonitorInventory class.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,174
  • Joined: 20-September 08

Re: Trouble populating my GUI window

Posted 11 March 2012 - 12:36 PM

Nope, you put it in Monitor
Was This Post Helpful? 1
  • +
  • -

#5 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble populating my GUI window

Posted 11 March 2012 - 12:45 PM

View Postg00se, on 11 March 2012 - 12:36 PM, said:

Nope, you put it in Monitor

OK The program runs and outputs correctly in the command window but I get an empty GUI window

I know that is because I have not told it to put any thing in it. I have simply built the window the problem is I do not know how to put anything in it.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,174
  • Joined: 20-September 08

Re: Trouble populating my GUI window

Posted 11 March 2012 - 12:50 PM

Use your text area. Append each Monitor instance using toString to it


ta.append(m.toString());

Was This Post Helpful? 1
  • +
  • -

#7 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble populating my GUI window

Posted 11 March 2012 - 12:54 PM

View Postg00se, on 11 March 2012 - 12:50 PM, said:

Use your text area. Append each Monitor instance using toString to it


ta.append(m.toString());

OK I kind of follow you on this except I am unsure where to do it. Do I put them in the MonitorGUI class?
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,174
  • Joined: 20-September 08

Re: Trouble populating my GUI window

Posted 11 March 2012 - 12:57 PM

The problem you're having now is because you've made the inventory class an application. Don't. Get rid of the main method and make its Monitor[] available to your presentation application
Was This Post Helpful? 0
  • +
  • -

#9 tray2475  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-January 12

Re: Trouble populating my GUI window

Posted 11 March 2012 - 01:01 PM

View Postg00se, on 11 March 2012 - 12:57 PM, said:

The problem you're having now is because you've made the inventory class an application. Don't. Get rid of the main method and make its Monitor[] available to your presentation application

Ok so you are saying that I need to put the main in my MonitorGUI class and then create an instance for the Monitor []?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1