Results 1 to 6 of 6
  1. #1
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default JList to JTextArea transfer

    Im trying to write a program that is GUI based and mimicks a department store. When the user clicks on the String on the JList it gets transfered to the text area.the ListActionListener is just using the underGarment list soi can figure it out how to make it work. can any one figure out what im doing wrong?
    Java Code:
    package onlineStore;
    
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class MensClothing extends JFrame {
    	private JPanel shirts, pants, shoes, underWear, shoppingCart, buttonPanel;
    	private JTabbedPane departments;
    	protected JList shirt, pant, shoe, underGarment;
    	protected JTextArea cart;
    
    	private String[] shirtList = new String[5];
    	private String[] pantList = new String[5];
    	private String[] shoeList = { "boot", "casual", "dress", "tennis",
    			"running" };
    	private String[] underWearList = { "titeyWhitey", "boxer", "boxerBrief",
    			"6 socks", "10 socks" };
    	ArrayList<String> itemChosen = new ArrayList<String>();
    	private final int WINDOW_WIDTH = 600;
    	private final int WINDOW_HEIGHT = 400;
    
    	public MensClothing() {
    
    		setTitle("Online store");
    		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    		setLayout(new GridLayout(1, 3));
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		departments = new JTabbedPane();
    
    		
    		buildShirtPanel();
    		// try{
    		// buildPantsPanel();
    		// }catch(IOException e){
    		// e.printStackTrace();
    		// }
    		buildShoesPanel();
    		buildUnderWearPanel();
    		
    
    		departments.addTab("Shirts", shirts);
    		departments.addTab("Pants", pants);
    		departments.addTab("Shoes", shoes);
    		departments.addTab("Under Wear", underWear);
    
    		add(departments);
    		add(buttonPanel);
    		buildShoppingCartPanel();
    		setVisible(true);
    	}
    
    	
    
    	public void buildShirtPanel() {
    		shirts = new JPanel();
    		shirt = new JList(shirtList);
    		shirts.add(shirt);
    
    	}
    
    	public void buildPantsPanel() throws IOException {
    		FileReader freader;
    		BufferedReader inputFile;
    		int i = 1;
    		pants = new JPanel();
    
    		freader = new FileReader("pants.txt");
    		inputFile = new BufferedReader(freader);
    
    		pantList[0] = inputFile.readLine();
    
    		while (inputFile != null) {
    
    			pantList[i] = inputFile.readLine();
    			i++;
    		}
    		pant = new JList(pantList);
    		pants.add(pant);
    	}
    
    	public void buildShoesPanel() {
    		shoes = new JPanel();
    		shoe = new JList(shoeList);
    		shoes.add(shoe);
    	}
    
    	public void buildUnderWearPanel() {
    		underWear = new JPanel();
    
    		underGarment = new JList(underWearList);
    		underGarment.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		underGarment.setSelectedIndex(0);
    		underGarment.addListSelectionListener(new ListSelector());
    		underWear.add(underGarment);
    	}
    
    	public void buildShoppingCartPanel() {
    		String [] items=new String[itemChosen.size()];
    		int i=0;
    		JList cart2=new JList();
    		shoppingCart = new JPanel();
    		if(itemChosen.size()>0){
    		for(String item: itemChosen){
    			items[i]=item;
    			i++;
    		System.out.println("added");	
    		}
    		cart2=new JList(items);
    		i=0;
    		}
    		
    		shoppingCart.setBackground(Color.WHITE);
    		shoppingCart.add(cart2);
    		add(shoppingCart);
    	}
    
    	private class ListSelector implements ListSelectionListener {
    		public void valueChanged(ListSelectionEvent e) {
    			itemChosen.add(underWearList[underGarment.getSelectedIndex()]);
    			buildShoppingCartPanel();
    		}
    
    	}
    
    	
    	public static void main(String[] args) {
    		MensClothing test = new MensClothing();
    
    	}
    }
    i commented some things, that can just be ignored.
    Last edited by timkd127; 03-06-2009 at 05:39 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    for one, you're adding a buttonPanel to the GUIthat has not been initialized causing a NPE to be thrown.

  3. #3
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    sry, i thought i had taken that out. thats not supposed to be there.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    problem number 2: what the heck is the method buildShoppingCartPanel() supposed to be doing? It appears to be trying to create a new JPanel each time that the jlist is clicked and adding this new JPanel to the JFrame somewhere...?

    problem number 3: you state that you're adding text to a text area, but while I see a JTextArea declared in your program, it is never constructed, never added to the program and never used.

    I'd recommend that you either give a much better explanation of just what you think this code is supposed to be doing, or rewrite this code (from the ground up) so that it does what you state above.

  5. #5
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    i think im gonna take the latter advice and completely rewrite it, i originally tried to do something else and than changed my thinking.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, sometimes it's best just to put the beast down rather than let it suffer a long, lingering and painful death. Then start afresh from a clean slate. Best of luck and goodnight.

Similar Threads

  1. file transfer
    By prashant in forum Networking
    Replies: 3
    Last Post: 12-10-2009, 07:46 AM
  2. Java File Transfer
    By lks0912 in forum Advanced Java
    Replies: 0
    Last Post: 10-23-2008, 01:53 PM
  3. Hosted Image Won't Transfer
    By StillLearning in forum New To Java
    Replies: 6
    Last Post: 07-19-2008, 04:21 AM
  4. Fast Data Transfer 0.8.5
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-12-2007, 07:11 PM
  5. Fast Data Transfer 0.6.7
    By Jamie in forum Java Software
    Replies: 0
    Last Post: 06-14-2007, 03:55 PM

Posting Permissions

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