Results 1 to 2 of 2
  1. #1
    Avey is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Exclamation How do I manipulate these objects in GUI handling

    I am making a simply grocery cart app that is due really soon, I'm stressing out about it.

    I can't figure out how I would use the objects I'm reading from a file within the handling methods of my app.

    Right now it outputs the toString of each object because the toString methods are being called in the main.
    Then the JFrame appears, and when selecting a string from the list I get the wrong prices, is there a better way of doing this?

    How can I use an object variable initialized in the main and use that same object in the handling methods?

    Any suggestions are greatly appreciated. Thank you.



    Java Code:
    package Apps;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import java.util.Scanner;
    
    import Products.Fruit;
    import Products.Pharmacy;
    import Products.Shoes;
    import Products.TVS;
    import java.io.*;
    import java.util.*;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class ProductDatabaseRead extends JFrame implements ActionListener, ListSelectionListener {
    
    	private static final int FRAME_WIDTH	= 500;
    	private static final int FRAME_HEIGHT	= 400;
    	private static final int FRAME_X_ORIGIN	= 500;
    	private static final int FRAME_Y_ORIGIN	= 250;
    
    	private static final int BUTTON_WIDTH	= 80;
    	private static final int BUTTON_HEIGHT	= 30;
    	
    	public static JList list;
    	public static JPanel listPanel;
    	public static JPanel descriptionPanel;
    	public static JTextArea textArea;
    	public static JScrollPane scrollPane;
    	public static JPanel shoppingCartPanel;
    	public static JButton purchaseButton;
    	public static JButton cartButton;
    	public static JButton buyButton;
    	public static JButton completeButton;
    	public static JButton exitButton;
    	
    	Object[] itemsSelected;
    	Object[] itemsPurchased;
     
    	Container content = getContentPane();   // get my canvas
    	
    	String[] listOptions = {"Fruit","Shoe", "Pharmacy", "TVS"};
    	
    	public static Pharmacy ph;
    	public static TVS tv;
    	public static Shoes shoe;
    	public static Fruit fruit;
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		
    		Object o = null;
    		
    		try
    		{
    		ObjectInputStream input = new ObjectInputStream(new FileInputStream("Storage.dat"));
    		
    		boolean done = false;
    		
    		System.out.println(" opened file");
    		
    		while (!done)
    		{
    			o = input.readObject();
    
    			if (o instanceof Pharmacy)
    			{
    				ph = (Pharmacy) o;
    				
    				System.out.println(" The pharmacy object: " + ph.toString());
    				
    				System.out.println(" The inventory is " + ph.getInventory());
    
    				ph.setInventory(45);  // inventory can be changed in the object this way
    				
    				System.out.println(" The inventory is changed");  // change is not on the file, need to write to the object file
    				
    				
    			}
    			if (o instanceof TVS)
    			{
    				tv = (TVS) o;
    				
    				System.out.println(" The TV object: " + tv.toString());
    				
    				
    				
    			}
    			if (o instanceof Shoes)
    			{
    				shoe = (Shoes) o;
    				
    				System.out.println(" The Shoe object: " + shoe.toString());
    				
    				
    				
    			}
    			if (o instanceof Fruit)
    			{
    				fruit = (Fruit) o;
    				
    				System.out.println(" The Fruit object: " + fruit.toString());
    				
    				
    				
    			}
    			
    		}
    		
    		
    		
    		}
    		catch (Exception e)
    		{
    	//		System.out.println(e.toString());
    			if (e instanceof EOFException)
    				System.out.println("Exception occurred - ending - no more objects to read");
    		}
    		
    		ProductDatabaseRead screen = new ProductDatabaseRead();
    		
    	}
    	
    	public ProductDatabaseRead()
    	{
    		setSize(FRAME_WIDTH,FRAME_HEIGHT); 	 // window size
    		setTitle(" Online Store ");
    		setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLayout(new BorderLayout());
    		
    		Dimension dim = new Dimension(100,30);
    		
    		list = new JList(listOptions);
    		listPanel = new JPanel();
    		list.setSize(dim);
    		listPanel.add(new JScrollPane(list));
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		list.addListSelectionListener(this);
    		content.add(listPanel, BorderLayout.WEST);
    		
    		dim = new Dimension(80,30);
    		
    		textArea = new JTextArea();
    		textArea.setColumns(20);
    		textArea.setRows(10);
    		textArea.setEditable(false);
    		scrollPane = new JScrollPane(textArea);
    		content.add(scrollPane, BorderLayout.EAST);
    		
    		descriptionPanel = new JPanel();
    		descriptionPanel.setBackground(getBackground());
    		content.add(descriptionPanel, BorderLayout.NORTH);
    		
    		
    		cartButton = new JButton();
    		cartButton.setText("Cart");
    		cartButton.setPreferredSize(dim);
    		content.add(cartButton, BorderLayout.CENTER);
    		cartButton.addActionListener(this);
    
    		exitButton = new JButton();
    		exitButton.setText("Exit");
    		exitButton.setPreferredSize(dim);
    		content.add(exitButton, BorderLayout.SOUTH);
    		exitButton.addActionListener(this);
    		
    		purchaseButton = new JButton();
    		purchaseButton.setText("Purchase");
    		purchaseButton.setPreferredSize(dim);
    		content.add(purchaseButton, BorderLayout.NORTH);
    		purchaseButton.addActionListener(this);
    
    		buyButton = new JButton();
    		buyButton.setText("Buy");
    		buyButton.setPreferredSize(dim);
    		buyButton.addActionListener(this);
    		
    //		getContentPane().add(new JScrollPane(list));
    		
    		
    		content.setBackground(Color.black); //set background whatevs
    		
    		
    		
    		
    		
    		setVisible(true);
    		
    	}
    	
    	public void actionPerformed(ActionEvent event) {
    		
    		JButton buttonClicked = (JButton) event.getSource();
    		
    		if (buttonClicked.getText() == "Exit")
    		{
    			System.out.println("Exit");
    			System.exit(0);
    		}
    		if(buttonClicked.getText() == "Cart")
    		{
    			System.out.println("Cart");
    			
    			JFrame cart = new JFrame();
    			cart.setSize(FRAME_WIDTH,FRAME_HEIGHT); 	 // window size
    			cart.setTitle(" Your Cart ");
    			cart.setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    			cart.setDefaultCloseOperation(EXIT_ON_CLOSE);
    			cart.setLayout(new BorderLayout());
    			
    			Container cartContent = getContentPane(); 
    			
    			descriptionPanel = new JPanel();
    			descriptionPanel.setBackground(getBackground());
    			cartContent.add(descriptionPanel, BorderLayout.WEST);
    			cartContent.add(exitButton, BorderLayout.CENTER);
    			cartContent.add(buyButton, BorderLayout.AFTER_LAST_LINE);
    
    			buyButton.addActionListener(this);
    			
    			cart.setVisible(true);
    			
    		} 
    		if(buttonClicked.getText() == "Purchase")
    		{
    			System.out.println("Purchase");
    		}
    	}
    	
    	public void valueChanged(ListSelectionEvent e) {	
    		
    		//this is where I need help, do I have to make the valueChanged method pass an object to manipulate it?
    		
    		
    		if (! e.getValueIsAdjusting())
    		{
    		int itemNumber = list.getSelectedIndex();
    		
    			if(itemNumber == 0)
    			{
    				textArea.append(list.getSelectedValue() + ": $" + fruit.getPrice() + "\n");
    			} 
    			if(itemNumber == 1)
    			{
    				textArea.append("A " + list.getSelectedValue() + " is $" + shoe.getPrice() + "\n");
    			} 
    			if(itemNumber == 3)
    			{
    				textArea.append("A " + list.getSelectedValue() + " is $" + ph.getPrice() + "\n");
    			} 
    			if(itemNumber == 4)
    			{
    				textArea.append("A " + list.getSelectedValue() + " is $" + tv.getPrice() + "\n");
    			} 
    		}
    	}
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: How do I manipulate these objects in GUI handling

    Hi Avey,

    Instead of calling the getPrice() method have you tried calling the toString() method. This will confirm if the method is seeing the instance of if there is something wrong with the getPrice() method.

    Regards.

Similar Threads

  1. Re: [HELP] How to manipulate jTable?
    By alan2 in forum Suggestions & Feedback
    Replies: 3
    Last Post: 03-25-2013, 08:43 AM
  2. [HELP] How to manipulate jTable?
    By daemon05 in forum NetBeans
    Replies: 4
    Last Post: 03-25-2013, 08:41 AM
  3. JSF Manipulate onclick value
    By alafarm in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-28-2012, 10:17 PM
  4. Replies: 10
    Last Post: 04-05-2012, 09:32 AM
  5. How to manipulate JtextArea
    By bachtoutou in forum New To Java
    Replies: 8
    Last Post: 05-22-2008, 09:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •