Get Methods and GUI

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 2346 Views - Last Post: 23 June 2009 - 05:12 PM Rate Topic: -----

#16 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 19 June 2009 - 09:39 PM

You can get those methods from somewhere at the top of this post, although they might need changing a bit. A JTextField isnt the best option to display these items, unless you make it uneditable. All you need to do is get the correct position in the Array, use your get methods and place them in the JTextField using the appropiate method in the api, setText maybe.
Was This Post Helpful? 0
  • +
  • -

#17 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 19 June 2009 - 09:47 PM

Thanks for all the help! I'm understanding this stuff better now, but I'm not out of the woods yet.
Was This Post Helpful? 1

#18 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 20 June 2009 - 01:16 PM

Now I'm stuck trying to get this program to add the Total InventoryValue. I can get it to work, but it runs a loop and won't terminate :blink: Also, I tried to get the program to send the Restocking Fee to the toString, but that doesn't seem to work. Any ideas?

This is what I put into my Inventory.java file for the Total Inventory Value:
public double getTotalInvValue()
	{
		double sumOfInventory = 0.0;//declare and initialize variable to 0.0
	   
		// Uses a condensed for loop which iterates the array of items.
		for (Product item : items)
		{
			// Make sure we have an item for the given position.
			if (item != null)
			{
				sumOfInventory += item.getItemValue();
			}
		}
		return sumOfInventory;
	}
	
	public void printTotalInvValue()
	{
		System.out.println("Printing total value of inventory...\n");

		for ( int x=0; x<items.length; x++ )
		{
			System.out.println( getTotalInvValue() );
		}

And this is what I put into the DVD.java file because it is subclass of Product and they both send toString for the array in Inventory.
public String toString()
	{
		String details = super.toString();
		details += "Movie Date " + movieyear +"\n" + "Restocking Fee " + getRestockingFee() + "\n";
		return details;
	}

Was This Post Helpful? 0
  • +
  • -

#19 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 20 June 2009 - 04:48 PM

Alright. I finally got the code to work... kinda. It repeats the TotalInvValue 5 times when ran, like it puts that method through the array or something. The math is right for the whole Inventory Total.. it just displays it 5 times. Also, I got the restocking fee to return but it is a double and goes out 10 digits when it displays the restocking fee. How do I shorten that to only .2? The "%.2d only works for Printf? Now I'm on to trying and get this into my GUI.
Was This Post Helpful? 0
  • +
  • -

#20 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 20 June 2009 - 06:36 PM

When doing your looping in your method, there is no real need for you to use an extended for loop. Just do the usual x=0; x<items.length.....

As for the shortening, you use somthing called a DecimalFormat. So you do somthing like
DecimalFormat sterling = new DecimalFormat( "0.00" );


And when you come to print it out, If i remember right you do
String output = sterling.format(sumOfInventory);
System.out.println(output);

Was This Post Helpful? 0
  • +
  • -

#21 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 21 June 2009 - 06:54 PM

Well, I've been trying to get my GUI TextFields to use my Inventory class's 'get' methods to get the data. So far, unsuccessfully. I created an object of Inventory inside my InventoryGUI file. Inventory myWork = new Inventory(); but that didn't work either. Would anyone know the proper way to have my data return to the TextField?
Was This Post Helpful? 0
  • +
  • -

#22 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 21 June 2009 - 07:05 PM

show what your trying, prob need to post the inventory class and gui class.
Was This Post Helpful? 0
  • +
  • -

#23 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 21 June 2009 - 07:19 PM

 import java.text.DecimalFormat;

// Inventory class, used to manage a bunch of Product classes.

public class Inventory
{
	// Setup an array of Products (set it to hold 5 items)
	int inventorySize = 5;
	private Product[] items = new Product[inventorySize];//set constant size of 5 to new product as argument.

	public void defaultData()
	{
		DVD dvd1 = new DVD( 001, "Saw", 4, 9.95, 2002);
		items[0] = dvd1;
	   
		DVD dvd2 = new DVD( 002, "Saw 2", 2, 5.99, 2003 );
		items[1] = dvd2;
	   
		DVD dvd3 = new DVD( 003, "Saw 3", 3, 7.39, 2004 );
		items[2] = dvd3;
	   
		DVD dvd4 = new DVD( 004, "Saw 4", 8, 15.99, 2006 );
		items[3] = dvd4;
	   
		DVD dvd5 = new DVD( 005, "Saw 5", 7, 15.99, 2008 );
		items[4] = dvd5;
	}
 
	// Prints the inventory list including name, quantity, price, and total stock value for each item.
	public void printInventory()
	{
		System.out.println("Printing items in inventory...\n");

		for (int x=0; x<items.length; x++)
		{
			System.out.println(items[x].toString());
		}

	}
	// Loop through array of products and add up the total value.
	// Go item by item adding the quantity on hand * its price.
	// Add that value to a running total accumulator variable.

	public double getTotalInvValue()
	{
		double sumOfInventory = 0.0;//declare and initialize variable to 0.0
	   
		// Uses a condensed for loop which iterates the array of items.
		for (Product item : items)
		{
			// Make sure we have an item for the given position.
			if (item != null)
			{
				sumOfInventory += item.getItemValue();
			}
		}
		return sumOfInventory;
	}
	
	public void printTotalInvValue()
	{
		System.out.println("Printing total value of inventory...\n");

		for ( int x=0; x<items.length; x++ )
		{
			System.out.println( getTotalInvValue() );
		}

	}

	public static void main(String args[])
	{
		Inventory myWork = new Inventory();
		myWork.defaultData();
		myWork.printInventory();
		myWork.printTotalInvValue();
	}
}



// InventoryGUI.java
//Using the JLabel class to display Inventory program

import java.awt.*; // specifies how components are arranged
import javax.swing.*; //see if the asterisk loads all that stuff.
import java.text.DecimalFormat; //decimalFormat loaded just in case it needs it to display properly

public class InventoryGUI extends JFrame //get parent class JFrame and use for new class InventoryGUI
{	
	private static final int WIDTH = 600;
	private static final int HEIGHT = 150;
	
	//private JPanel jpOuterPanel; // For the outermost frame	
	
	private JLabel itemNameL, quantityOnHandL, itemPriceL, productIdL, yearL, welcome1L, welcome2L, itemValueL, totalInvValueL,
					restockingFeeL;// Declare Labels to be used in GUI
	
	private JTextField itemNameTF, quantityOnHandTF, itemPriceTF, productIdTF, itemValueTF, totalInvValueTF,
					yearTF, restockingFeeTF;	// Declare TextFields to be used in GUI	
		
	//private JButton calculateB, exitB; //Declare buttons

	
	public InventoryGUI() //InventoryGUI constructor adds JLabels and JTextFields to JFrame
	{
		Inventory myWork = new Inventory();
		
		itemNameL = new JLabel( "Name" );
		quantityOnHandL = new JLabel( "Quantity on Hand" );
		itemPriceL = new JLabel( "Price per item" );
		productIdL = new JLabel( "Product ID" );
		itemValueL = new JLabel( "Value of all Same Items" );
		totalInvValueL = new JLabel( "Value of All Items" );
		yearL = new JLabel( "Year" );
		restockingFeeL = new JLabel( "Restocking Fee" );
		
		itemNameTF = new JTextField( myWork.getItemName(), 30 );
		quantityOnHandTF = new JTextField( 3 );
		itemPriceTF = new JTextField( 10 );
		productIdTF = new JTextField( 5 );
		itemValueTF = new JTextField( 10 );
		totalInvValueTF = new JTextField( 12 );
		yearTF = new JTextField( 10 );
		restockingFeeTF = new JTextField( 8 );
		
		setTitle( "Sample Title: Mike's Inventory" );
		Container pane = getContentPane();
		pane.setLayout( new FlowLayout() );
		
		pane.add( itemNameL );
		pane.add( itemNameTF );
		pane.add( quantityOnHandL );
		pane.add( quantityOnHandTF );
		pane.add( itemPriceL );
		pane.add( itemPriceTF );
		pane.add( productIdL );
		pane.add( productIdTF );
		pane.add( itemValueL );
		pane.add( itemValueTF );
		pane.add( totalInvValueL );
		pane.add( totalInvValueTF );
		pane.add( yearL );
		pane.add( yearTF );
		pane.add( restockingFeeL );
		pane.add( restockingFeeTF );
					   
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	 public static void main(String args[])
	{
		InventoryGUI myWork = new InventoryGUI();			   
	}
}   




I got the GUI to show all the labels with the textfields next to them and all. But I've tried all different kinds of ways to try and get that get methods to call. This only has the first TextField with my last idea. If I can get that one to work, the rest I can add in when I know how. My book showed my something like getClass().getResource() or something like that... But I couldn't get that to work either.
Was This Post Helpful? 0
  • +
  • -

#24 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 21 June 2009 - 08:00 PM

k, first thing you need to do is remove your main from your inventory class. You dont have 2 main methods in one application. Your going to have to call the default data mthod prob under like this in your gui class
Inventory myWork = new Inventory();
myWork.defaultData();


Now, its your Array in your inventory class which holds the data you need, so somehow you need to get that Array to your gui class. To do this, I would just add a getArray method in your inventory class, somthing like
public Product[] getArray(){
return this.items;
}


Then in your gui class, where you load your default data, do somthing like
Product[] items = myWork.getArray();


So now you should have the Array in your gui class. Now to get the data into your JTextFields, you can just use the setText method provided by the api. Now I dont know how you want to change the items in the JTextField, but just so you can see it working for now, do somthing like
itemNameTF = new JTextField(10);
itemNameTF.setText(items[0].getName());


I am hoping that works but i havnt checked it out. To get them changed, you might have to take a different approach, maybe using switch statements
Was This Post Helpful? 0
  • +
  • -

#25 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 21 June 2009 - 09:00 PM

You gave me some great ideas. I understand why some of these things are done now. I redid my code like you said, and my Inventory file compiles, but the GUI class doesn't. I get this error:
javac InventoryGUI.java
Process started >>>
InventoryGUI.java:38: cannot find symbol
symbol  : variable items
location: class InventoryGUI
		itemNameTF.setText( items[0].getName() );
							^
1 error


So does this mean that array "items" isn't in the GUI class? That was the right method to return the name and everything. I don't know, except maybe the array didn't load properly? I'm not sure what the problem is and I've tried a few different things, mainly name changes, but to no avail. Any ideas?
Was This Post Helpful? 0
  • +
  • -

#26 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 21 June 2009 - 09:06 PM

post ur updated gui class
Was This Post Helpful? 0
  • +
  • -

#27 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 21 June 2009 - 09:35 PM

 // InventoryGUI.java
//Using the JLabel class to display Inventory program

import java.awt.*; // specifies how components are arranged
import javax.swing.*; //see if the asterisk loads all that stuff.
import java.text.DecimalFormat; //decimalFormat loaded just in case it needs it to display properly

public class InventoryGUI extends JFrame //get parent class JFrame and use for new class InventoryGUI
{	
	private static final int WIDTH = 600;
	private static final int HEIGHT = 150;
	
	//private JPanel jpOuterPanel; // For the outermost frame	
	
	private JLabel itemNameL, quantityOnHandL, itemPriceL, productIdL, yearL, welcome1L, welcome2L, itemValueL, totalInvValueL,
					restockingFeeL;// Declare Labels to be used in GUI
	
	private JTextField itemNameTF, quantityOnHandTF, itemPriceTF, productIdTF, itemValueTF, totalInvValueTF,
					yearTF, restockingFeeTF;	// Declare TextFields to be used in GUI	
		
	//private JButton calculateB, exitB; //Declare buttons

	
	public InventoryGUI() //InventoryGUI constructor adds JLabels and JTextFields to JFrame
	{
		Inventory myWork = new Inventory();
		
		itemNameL = new JLabel( "Name" );
		quantityOnHandL = new JLabel( "Quantity on Hand" );
		itemPriceL = new JLabel( "Price per item" );
		productIdL = new JLabel( "Product ID" );
		itemValueL = new JLabel( "Value of all Same Items" );
		totalInvValueL = new JLabel( "Value of All Items" );
		yearL = new JLabel( "Year" );
		restockingFeeL = new JLabel( "Restocking Fee" );
		
		itemNameTF = new JTextField( 20 );
		itemNameTF.setText( items[0].getName() );
		
		quantityOnHandTF = new JTextField( 3 );
		itemPriceTF = new JTextField( 10 );
		productIdTF = new JTextField( 5 );
		itemValueTF = new JTextField( 10 );
		totalInvValueTF = new JTextField( 12 );
		yearTF = new JTextField( 10 );
		restockingFeeTF = new JTextField( 8 );
		
		setTitle( "Sample Title: Mike's Inventory" );
		Container pane = getContentPane();
		pane.setLayout( new FlowLayout() );
		
		pane.add( itemNameL );
		pane.add( itemNameTF );
		pane.add( quantityOnHandL );
		pane.add( quantityOnHandTF );
		pane.add( itemPriceL );
		pane.add( itemPriceTF );
		pane.add( productIdL );
		pane.add( productIdTF );
		pane.add( itemValueL );
		pane.add( itemValueTF );
		pane.add( totalInvValueL );
		pane.add( totalInvValueTF );
		pane.add( yearL );
		pane.add( yearTF );
		pane.add( restockingFeeL );
		pane.add( restockingFeeTF );
					   
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	  public static void main(String args[])
	{
		Inventory myWork = new Inventory();
		myWork.defaultData();			
		Product[] items = myWork.getArray();			   
	}
}   




I only added the main and the first TextField.
Was This Post Helpful? 0
  • +
  • -

#28 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 22 June 2009 - 02:01 AM

None of that stuff should be in your main method, it should be in your class constructor. You main method should only have this
	public static void main(String[] args){ 
	SwingUtilities.invokeLater(new Runnable(){
		public void run(){
			new InventoryGUI();
		}
	});
	
}


Then your constructor should do somthing like
 public InventoryGUI() //InventoryGUI constructor adds JLabels and JTextFields to JFrame
	{
		Inventory myWork = new Inventory();
		myWork.defaultData();			
		Product[] items = myWork.getArray();	

Was This Post Helpful? 0
  • +
  • -

#29 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 22 June 2009 - 08:51 PM

Well, you were right, and those things did work. The problem I'm having now is that some of the TextFields still can't find some of the "get"methods, and TextField won't work with "int" and "double" so I need to convert those to strings. Here is my updated InventoryGUI class. I commented out the lines that were giving compiler errors and noted the reason why. Any ideas?
// InventoryGUI.java
//Using the JLabel class to display Inventory program

import java.awt.*; // specifies how components are arranged
import javax.swing.*; //see if the asterisk loads all that stuff.
import java.text.DecimalFormat; //decimalFormat loaded just in case it needs it to display properly

public class InventoryGUI extends JFrame //get parent class JFrame and use for new class InventoryGUI
{	
	private static final int WIDTH = 600;
	private static final int HEIGHT = 150;
	
	//private JPanel jpOuterPanel; // For the outermost frame	
	
	private JLabel itemNameL, quantityOnHandL, itemPriceL, productIdL, yearL, welcome1L, welcome2L, itemValueL, totalInvValueL,
					restockingFeeL;// Declare Labels to be used in GUI
	
	private JTextField itemNameTF, quantityOnHandTF, itemPriceTF, productIdTF, itemValueTF, totalInvValueTF,
					yearTF, restockingFeeTF;	// Declare TextFields to be used in GUI	
		
	//private JButton calculateB, exitB; //Declare buttons

	
	public InventoryGUI() //InventoryGUI constructor adds JLabels and JTextFields to JFrame
	{
		Inventory myWork = new Inventory();
		myWork.defaultData();			
		Product[] items = myWork.getArray();	
		
		itemNameL = new JLabel( "Name" );
		quantityOnHandL = new JLabel( "Quantity on Hand" );
		itemPriceL = new JLabel( "Price per item" );
		productIdL = new JLabel( "Product ID" );
		itemValueL = new JLabel( "Value of all Same Items" );
		totalInvValueL = new JLabel( "Value of All Items" );
		yearL = new JLabel( "Year" );
		restockingFeeL = new JLabel( "Restocking Fee" );
		
		itemNameTF = new JTextField( 20 );
		itemNameTF.setText( items[0].getName() );
		
		quantityOnHandTF = new JTextField( 3 );
		//quantityOnHandTF.setText( items[0].getQuantityOnHand() );  TextField can't be applied to "int"
		
		itemPriceTF = new JTextField( 10 );
		//itemPriceTF.setText( items[0].getPrice() );  TextField can't be applied to "double"
		
		productIdTF = new JTextField( 5 );
		//productIdTF.setText( items[0].getProductId() ); can't find in Product class
		
		itemValueTF = new JTextField( 10 );
		//itemValueTF.setText( items[0].getItemValue() );  TextField can't do "double"
		
		totalInvValueTF = new JTextField( 12 );
		//totalInvValueTF.setText( items[0].getTotalInvValue() );  can't find in Product class
		
		yearTF = new JTextField( 10 );
		//yearTF.setText( items[0].getYear() );  can't find in  Product class
		
		restockingFeeTF = new JTextField( 8 );
		//restockingFeeTF.setText( items[0].getRestockingFee() );  can't find in Product class
		
		setTitle( "Mike's Inventory" );
		Container pane = getContentPane();
		pane.setLayout( new FlowLayout() );
		
		pane.add( itemNameL );
		pane.add( itemNameTF );
		pane.add( quantityOnHandL );
		pane.add( quantityOnHandTF );
		pane.add( itemPriceL );
		pane.add( itemPriceTF );
		pane.add( productIdL );
		pane.add( productIdTF );
		pane.add( itemValueL );
		pane.add( itemValueTF );
		pane.add( totalInvValueL );
		pane.add( totalInvValueTF );
		pane.add( yearL );
		pane.add( yearTF );
		pane.add( restockingFeeL );
		pane.add( restockingFeeTF );
					   
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String[] args)
	{
		SwingUtilities.invokeLater( new Runnable(){ public void run(){ new InventoryGUI(); } });
   
	}
}   




On a side note, how does that invokeLater thing with the new Runnable thing work? Is that a like a new main?

This post has been edited by Galnoth: 22 June 2009 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#30 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 22 June 2009 - 09:05 PM

Anywhere it cant find it in Product class, this means you dont have a get method for that in your Product class. This means you will have to go back to your Product class and add the get methods like how you have your other get methods in there.
As not not being able to display int or double, or you need to do is cast them to a String. Pretty straight forward.
And the invoke thing in your main just calls instantiates the constructor of your gui class. Its quite a common way of doing it in gui applications.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3