Results 1 to 3 of 3

Thread: Two JLists

  1. #1
    Psygnosys is offline Overboard!
    Join Date
    Apr 2012
    Location
    Manchester/Aberystwyth, UK
    Posts
    4
    Rep Power
    0

    Question Two JLists

    Hi, I'm a first year computer science student, working on my project Easter in my programming module. The idea of the project is to create a till machine for a pizza company.

    I am trying to create two JLists, side by side. The first JList specifies the categories - pizza, drink, side - & the second list shows the products available in that category. The results of these two selections are then used & passed on to a method to add the product to the order, after hitting a JButton.

    The problem is, the user cannot select a product on the second list, therefore the methods in the ListSelectionListener's valueChanged method don't get called for the second list. It's obvious that they do the for the first, as the product list displays the products.

    Two JLists-jlists-selecting-issue.jpg

    The second list doesn't look properly selected.

    My code:


    The part in the constructor which does the lists:



    Java Code:
                    typeList=new JList(type);
    		typeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		JScrollPane typeListScroller=new JScrollPane(typeList);
    		typeList.addListSelectionListener(this);
    		this.add(typeListScroller);
    
    		productList=new JList();
    		productList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		JScrollPane productListScroller=new JScrollPane(productList);
    		productList.addListSelectionListener(this);
    		this.add(productListScroller);

    The listener method:


    Java Code:
    public void valueChanged(ListSelectionEvent e) {
    		if(typeList.getSelectedIndex()==0){
    			productList.setListData(drinkList);
    			if(productList.getSelectedIndex()==0){ 
                            System.out.println("Please work!")
    			}
    		}
    		else if(typeList.getSelectedIndex()==1){
    			productList.setListData(pizzaList);
    		}
    		else if(typeList.getSelectedIndex()==2){
    			productList.setListData(sideList);
    		}
    	}



    I'm just wondering if anyone has any idea on how to tackle the problem? I could do it a different way, but I quite like the way this looks [aesthetically].

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Two JLists

    Can you post a small program that compiles, executes and shows the problem? a SSCCE
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,251
    Rep Power
    19

    Default Re: Two JLists

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JLists
    By loopsnhoops in forum Advanced Java
    Replies: 3
    Last Post: 05-28-2011, 12:22 AM
  2. JLISTS and Classes
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 05-27-2011, 07:54 AM
  3. JLists and JTables
    By loopsnhoops in forum New To Java
    Replies: 2
    Last Post: 05-27-2011, 01:19 AM
  4. Question about JLists
    By Psyclone in forum AWT / Swing
    Replies: 4
    Last Post: 02-21-2010, 03:56 AM
  5. JLists - Filtering
    By Psyclone in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2010, 06:09 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
  •