3 Replies - 571 Views - Last Post: 01 August 2008 - 09:25 AM Rate Topic: -----

#1 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

How do I make GUI work with my array?

Post icon  Posted 01 August 2008 - 04:38 AM

OK so, now I am supposed to start Part five of my inventory assignment, and I am still stuck on part 4 which was already graded a week ago. I have it started but can't get started on making it work with my array. I am supposed to override my event handlers, but the examples my instructor gives do not help me at all, I can't even see how they relate to my code. I need to make my GUI function...so when I click on Pinnochio...It should display that info...etc. as well as being able to click the next button to navigate the selections.

I understand the concept and theory of all of this, I am just having a really hard time putting this assignment together. (Especially when my professor says " i don't know how to help you figure this out without just telling you how to do it"....Well.....since I already got my (bad) grade on the assignment what's the harm in showing me where I am wrong, or at least helping me get started so I can learn and possibly be ready for the next part of the assignment

If someone can help me get started on the implementation of my array i would be forever grateful......

here's my code so far

*
* DVDJPanel.java
*
* Created on July 30, 2008, 11:19 AM
*/

package inventory3;
import javax.swing.JOptionPane;

/**
*
* @author  Stacey
*/
class inventory3 {

	   public static void main(String[] args) {
		Inventory inventory = new Inventory();
		Movie dvd;

		dvd = new Movie("DVD1", 2356, "Pinnochio", 25, 14.99);
		inventory.add(dvd);
		dvd = new Movie("DVD2", 5684, "Shark Tale", 3, 12.00);
		inventory.add(dvd);
		dvd = new Movie("DVD3", 5564, "Flushed Away", 15, 15.75);
		inventory.add(dvd);
		dvd = new Movie("DVD4", 5562, "Shrek", 7, 9.99);
		inventory.add(dvd);

		inventory.display();
	} //end main

} // end class Inventory3


class DVD {
	private int ItemNumber;
	private String ProductName;
	private int units;
	private double unitPrice;
	private double inventoryvalue;
	private double TotalStockValue;
	
	public DVD(int item, String title, int stock, double price) {
		ItemNumber  = item;
		ProductName = title;
		units = stock;
		unitPrice = price;
	} //end four-argument constructor

	// set DVD Item
	public void setDvdItem(int item) {
		ItemNumber = item;
	} //end method  set Dvd Item

	//return DVD Item
	public int getItemNumber() {
		return ItemNumber;
	} //end method get Dvd Item

	//set DVD Title
	public void setProductName(String title) {
		ProductName = title;
	} //end method set Dvd Title

	//return Dvd Title
	public String getProductName() {
		return ProductName;
	} //end method get Dvd Title

	public void setDvdStock(int stock) {
		units = stock;
	} //end method set Dvd Stock

	//return dvd Stock
	public int getunits() {
		return units;
	} //end method get Dvd Stock

	public void setunitPrice(double price) {
		unitPrice = price;
	} //end method setdvdPrice

	//return DVD Price
	public double getunitPrice() {
		return unitPrice;
	} //end  method get Dvd Price

	//calculate inventory value
	public double inventoryvalue() {
		return unitPrice * units;
	} //end method value

	//calculate total stock value
	public double TotalStockValue(){
		return inventoryvalue += inventoryvalue;
		
	}
	@Override
	public String toString() {
		return String.format("item=%3d   title=%-20s   units=%3d   price=$%6.2f   value=$%7.2f",
							  ItemNumber, ProductName, units, unitPrice, inventoryvalue());
	}

} //end class DVD


class Movie extends DVD {
	private String movieTitle;

	public Movie(String title, int item, String dtitle, int stock, double price) {
		super(item, dtitle,  stock, price);
		movieTitle = title;
	}


	public double value() {
		double inventoryvalue = getunitPrice() * getunits();
		inventoryvalue = 1.05 * inventoryvalue;
		return inventoryvalue;
	} //end method value

	@Override
	public String toString() {
		String s = String.format("Movie title=%-12s", movieTitle);
		s = s + " " + super.toString();
		return s;
	}

} // end class Movie


class Inventory {
	private DVD[] dvds;
	private int count;
	private Object[] dvd;
	private double entireValue;

	Inventory() {
		dvds = new DVD[4];
		count = 0;
	}

	public void add(DVD dvd) {
		dvds[count] = dvd;
		++count;
		sort();
	}

	public double entireValue() {
		double value = 0;
		for (int i = 0; i < count; i++) {
		   value = value + ((Movie) dvds[i]).value();
		}
		return value;
	}

	public void sort() {
		for (int index = 1; index < count; index++) {
			DVD key = dvds[index];
			int position = index;

			//  Shift larger values to the right
			while (position > 0 && key.getProductName().compareTo(dvds[position-1].getProductName()) < 0) {
				dvds[position] = dvds[position-1];
				position--;
			}
			dvds[position] = key;
		}
	}

	public void display() {
		System.out.println("\nThe inventory contains " + count + " DVD titles.\n");
		for (int i = 0; i < count; i++)
			System.out.printf("%3d  %s\n", i, dvds[i]);
		System.out.printf("\nThe total inventory value is $%.2f\n\n", entireValue());
	}

} // end class Inventory

public class DVDJPanel extends javax.swing.JPanel {

	/** Creates new form DVDJPanel */
	public DVDJPanel() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jToggleButton6 = new javax.swing.JToggleButton();
		jToggleButton1 = new javax.swing.JToggleButton();
		jToggleButton2 = new javax.swing.JToggleButton();
		jToggleButton3 = new javax.swing.JToggleButton();
		jToggleButton4 = new javax.swing.JToggleButton();
		jToggleButton5 = new javax.swing.JToggleButton();
		jToggleButton7 = new javax.swing.JToggleButton();
		jPanel1 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextPane1 = new javax.swing.JTextPane();

		jToggleButton6.setText("jToggleButton6");

		jToggleButton1.setText("Pinnochio");
		jToggleButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jToggleButton1MouseClicked(evt);
			}
		});

		jToggleButton2.setText("Shark Tale ");
		jToggleButton2.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jToggleButton2MouseClicked(evt);
			}
		});

		jToggleButton3.setText("Flushed Away");
		jToggleButton3.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jToggleButton3MouseClicked(evt);
			}
		});

		jToggleButton4.setText("Shrek");
		jToggleButton4.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jToggleButton4MouseClicked(evt);
			}
		});

		jToggleButton5.setText("NEXT");

		jToggleButton7.setText("Total Value + restock fee");

		jTextPane1.addAncestorListener(new javax.swing.event.AncestorListener() {
			public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
			}
			public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
				jTextPane1AncestorAdded(evt);
			}
			public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
			}
		});
		jScrollPane1.setViewportView(jTextPane1);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
			jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGap(0, 501, Short.MAX_VALUE)
			.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE))
		);
		jPanel1Layout.setVerticalGroup(
			jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGap(0, 232, Short.MAX_VALUE)
			.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
		);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addContainerGap())
					.addGroup(layout.createSequentialGroup()
						.addComponent(jToggleButton1)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jToggleButton2)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jToggleButton3)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jToggleButton4)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jToggleButton7)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addComponent(jToggleButton5)
						.addGap(231, 231, 231))))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGap(27, 27, 27)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(jToggleButton1)
					.addComponent(jToggleButton2)
					.addComponent(jToggleButton3)
					.addComponent(jToggleButton4)
					.addComponent(jToggleButton7))
				.addGap(18, 18, 18)
				.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGap(18, 18, 18)
				.addComponent(jToggleButton5)
				.addContainerGap(19, Short.MAX_VALUE))
		);
	}// </editor-fold>

private void jToggleButton1MouseClicked(java.awt.event.MouseEvent evt) {											
// TODO add your handling code here:
}										   

private void jToggleButton2MouseClicked(java.awt.event.MouseEvent evt) {											
// TODO add your handling code here:
}										   

private void jToggleButton3MouseClicked(java.awt.event.MouseEvent evt) {											
// TODO add your handling code here:
}										   

private void jToggleButton4MouseClicked(java.awt.event.MouseEvent evt) {											
// TODO add your handling code here:
}										   

private void jTextPane1AncestorAdded(javax.swing.event.AncestorEvent evt) {
  
}


	// Variables declaration - do not modify
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextPane jTextPane1;
	private javax.swing.JToggleButton jToggleButton1;
	private javax.swing.JToggleButton jToggleButton2;
	private javax.swing.JToggleButton jToggleButton3;
	private javax.swing.JToggleButton jToggleButton4;
	private javax.swing.JToggleButton jToggleButton5;
	private javax.swing.JToggleButton jToggleButton6;
	private javax.swing.JToggleButton jToggleButton7;
	// End of variables declaration

}




Again Thank you for any help I might receive!
I am reading a bunch of books...Java for Dummies, Acomplete idiot's guide to Java, Java for beginners, Java in a nutshell, and of course my text book Java how to program..(which doesn't cover half the stuff they expect from us in class)....Hopefully I will be better prepared before I have to take my next Java class...Thank goodness this one is almost over!!!!!!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: How do I make GUI work with my array?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How do I make GUI work with my array?

Posted 01 August 2008 - 05:02 AM

Same DVDs.... look at the last post

http://www.dreaminco...wtopic58577.htm
Was This Post Helpful? 0
  • +
  • -

#3 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

Re: How do I make GUI work with my array?

Posted 01 August 2008 - 08:06 AM

View Postpbl, on 1 Aug, 2008 - 05:02 AM, said:

Same DVDs.... look at the last post

http://www.dreaminco...wtopic58577.htm



Thank you...This is helping me a lot, but I keep getting a message that reads:

Quote

Exception in thread "main" java.lang.NullPointerException
at dvdinventourygui.DVDInventoryGUI$Product.compareTo(DVDInventoryGUI.java:163)
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at dvdinventourygui.DVDInventoryGUI.<init>(DVDInventoryGUI.java:23)
at dvdinventourygui.DVDInventoryGUI.main(DVDInventoryGUI.java:48)
Java Result: 1


Here is my code that I have tried to fix per your example.... Thank you for your guidance
package dvdinventourygui;
import java.awt.*;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.Arrays;  
import javax.swing.*;  
  
public class DVDInventoryGUI{  
  
	DVDInventoryGUI() {	// constructor	
  
		Product[] dvd =  new Product[4];	
  
		dvd[0] = new Product(2356,"Pinnochio","G", 25, 14.99 );	
		dvd[1] = new Product(5684,"Shark Tale","G", 3, 12.00 );	
		dvd[2] = new Product(5564,"Flushed Away","G", 15, 15.75);	
		dvd[3] = new Product(5562,"Shrek","G", 7, 9.99);  
		Arrays.sort(dvd);	   
  
		int totalInStock = 0;  
		double totalValue = 0.0;   
		double ReStockingFee= 0.5;  
		for(int i = 0; i < 4; i++)   
		{	
  
			System.out.println("Item number:  " + dvd[i].getItemNumber());  
			System.out.println("Title: " + dvd[i].getproductName());  
			System.out.println("Rated: "+ dvd[i].getdvdRating());  
			System.out.println("In Stock: " + dvd[i].getdvdStock());	
			System.out.println("The price of each DVD is $" + dvd[i].getdvdPrice());  
			System.out.println("The value of the inventory is $" + dvd[i].Calvalue());	
			totalInStock += dvd[i].getdvdStock();  
			totalValue += dvd[i].Calvalue();  
			System.out.println("Total In Stock:" + totalInStock +   
					" Total Value:  $" +	  totalValue);  
			System.out.printf("Restock Fee: %.2f", dvd[i].Calvalue() * 0.05);	
		}  
		System.out.println();   
		new GUI(dvd);  
	}  
	public static void main(String args []) {

		DVDInventoryGUI dVDInventoryGUI = new DVDInventoryGUI();
  
	} //end method main	
  
	class Product implements Comparable	
	{  
		public int ItemNumber;   
		public String productName;  
		public String dvdRating;  
		public double dvdStock;  
		public double dvdPrice;  
		public double totalInStock;  
		public double totalValue;  
		public double ReStockFee;  

  
		// five-argument constructor  
		Product(double item,String title,double year, double stock, double price ,String Rating )   
		{	
			this.ItemNumber  = (int) item;  
			this.productName = title;   
			this.dvdRating = Rating;
			this.dvdStock = stock;	
			this.dvdPrice = price;	
		} //end five-argument constructor
		private Product(int i, String string, String string0, int i0, double d) {
		   
		}
		// set dvd item number  
		public void setItemNumber (int item)  
		{  
			ItemNumber = item;  
		}// end method setdvdItemNumber  
  
		// return dvd item number  
		public int getItemNumber()  
		{  
			return ItemNumber;  
		}// end method getdvdItemNumber  
  
		// set dvd title  
		public void setproductName (String title)  
		{  
			 this.productName = title;
		}// end method setdvdTitle  
  
		// return dvd title  
		public String getproductName()  
		{  
			return productName;  
		}
		   
		public void setdvdRating(String Rating)  
		{  
			dvdRating = Rating;  
		  }  
		public String getdvdRating()  
		{  
			return dvdRating;  
		}// end method getdvdRating 
  
		// set dvd stock  
		public void setdvdStock(double stock)  
		{  
			dvdStock= stock;  
		} // end method getdvdStock  
  
		// return dvd stock  
		public double getdvdStock()  
		{  
			return dvdStock;  
		}// end method getdvdStock  
  
		// set dvd price  
		public void setdvdPrice (double price)  
		{  
			dvdPrice= price;  
		}// end method setdvdPrice  
  
		// return dvd price  
		public double getdvdPrice()  
		{  
			return dvdPrice;  
		}// end method getdvdPrice  
  
		// set total in stock  
		public void settotalInStock (double total)  
		{  
			totalInStock= total;  
		}// end method settotalInStock  
  
		// return total in stock  
		public double gettotalInStock()  
		{  
			return totalInStock;  
		}// end method gettotalInStock  
  
		// caluclate Inventory  
		public double Calvalue() {	
			return dvdPrice * dvdStock;	
		} //end method value	
  
		// calculates Total Value  
		public double totalValue(){  
			return dvdPrice * totalInStock;  
		} // end method value  
  
		// calculates Re-Stocking Fee  
		public double ReStockingFee(){  
			return dvdPrice * totalInStock * .05;  
		}// end method value  
  
		// returns smaller, bigger or equals according to dvdTitle  
		public int compareTo(Object o){  
			Product other = (Product) o;  
			return productName.compareTo(other.productName);	
		}  
		@Override  
		public String toString()  
		{  
			return String.format( "%d\n,%s\n,%d\n,$%.2f\n,$%.2f,$%.2f ",  
					getproductName(), getItemNumber(), getdvdStock(), getdvdPrice(),Calvalue(),ReStockingFee() );  
  
		} // end method toString  
	}  
  
	class GUI extends JFrame implements ActionListener {  
		Product[] dvd;  
		JComboBox[] combo;  
		JLabel[] label;  
		JButton exitBtn;  
		boolean isAdjusting = false;  
		String[] comboHeader = {"Item #:", "Title:"};  
		String[] labelHeader = {"In stock:", "Price:", "Rating:"};  
  
		GUI(Product[] dvd) {  
			this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);   
			this.dvd = dvd;  
			setLayout(new BorderLayout());  
			  
		   
			JPanel panel = new JPanel(new GridLayout(5,2,5,5));  
			// combobox for item and title  
			combo = new JComboBox[2];  
			for(int i = 0; i < 2; i++) {  
				combo[i] = new JComboBox();  
			}  
			// label for price, stock, Rating  
			label = new JLabel[3];  
			for(int i = 0; i < 3; i++) {  
				label[i] = new JLabel();  
			}  
			// fill the combo  
			for(int i = 0; i < dvd.length; i++) {  
				combo[0].addItem("" + dvd[i].getItemNumber());  
				combo[1].addItem(dvd[i].getproductName());  
			}  
			// add the comboheader and the combos   
			for(int i = 0; i < 2; i++) {  
				// header  
				JLabel tmp = new JLabel(comboHeader[i]);  
				tmp.setForeground(Color.BLUE);  
				tmp.setHorizontalAlignment(SwingConstants.CENTER);  
				panel.add(tmp);  
				// combo  
				combo[i].setSelectedIndex(1);  
				combo[i].addActionListener(this);  
				panel.add(combo[i]);  
			}  
			// add the labels  
			for(int i = 0; i < 3; i++) {  
				// header  
				JLabel tmp = new JLabel(labelHeader[i]);  
				tmp.setForeground(Color.BLUE);  
				tmp.setHorizontalAlignment(SwingConstants.CENTER);  
				panel.add(tmp);  
				// display header  
				label[i] = new JLabel();  
				label[i].setHorizontalAlignment(SwingConstants.RIGHT);  
				panel.add(label[i]);  
			}  
			add(panel, BorderLayout.CENTER);  
			combo[0].setSelectedIndex(0);  
			  
		   
			JLabel tmp = new JLabel("DVD Inventory");  
			tmp.setHorizontalAlignment(SwingConstants.CENTER);  
			add(tmp, BorderLayout.NORTH);  
			// the exit button  
			exitBtn = new JButton("Exit");  
			exitBtn.addActionListener(this);  
			exitBtn.setForeground(Color.RED);  
			add(exitBtn, BorderLayout.SOUTH);  
			setSize(500, 40 * 5);  
			setVisible(true);  
		}  
  
		public void actionPerformed(ActionEvent e) {  
			if(isAdjusting)  
				return;  
			 Object o = e.getSource();  
			  
			// exit ?  
			if(o == exitBtn) {  
				this.dispose();  
				return;  
			}  
			int index = -1;  
			for(int i = 0; i < combo.length; i++) {  
				if(o == combo[i]) {  
					index = combo[i].getSelectedIndex();  
					break;  
				}  
			}  
			if(index == -1) {  
				isAdjusting = false;  
				return;  
			}  
			isAdjusting = true;  
			for(int i = 0; i < combo.length; i++) {  
				combo[i].setSelectedIndex(index);  
			}  
			// update labels  
			label[0].setText("" + dvd[index].getdvdStock() + "   ");  
			label[1].setText("$ " + dvd[index].getdvdPrice() + "   ");  
			label[2].setText("" + (String) dvd[index].getdvdRating() + "   ");
			label[3].setText("Total Stock Value including %5 restock fee is: $752.75");
			isAdjusting = false;  
		}  
	}  
  
} // end Product class   



Thank you for any help you can offer....I think I am starting to gain a better grasp on the subject=)
Was This Post Helpful? 0
  • +
  • -

#4 StaceyE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-July 08

Re: How do I make GUI work with my array?

Posted 01 August 2008 - 09:25 AM

I found some help, and after rewriting most af my program again, I finally got it to work ....so now I'm on to part 5...wish me luck and thanks for the pointers.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1