The Artima Developer Community
Sponsored Link

Java Answers Forum
JList problem

2 replies on 1 page. Most recent reply: Aug 5, 2003 10:51 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
eddie

Posts: 2
Nickname: e2dd
Registered: Jul, 2003

JList problem Posted: Aug 3, 2003 6:59 PM
Reply to this message Reply
Advertisement
i am passing the value of vector when it's JList is selected to another vector and another JList will display my new vector . But it appear 2 times, why ? Here's my code.

there are two seperated files for the vector. Foodmen and FoodOrd
 
Vector foodVec, ordfVec;
 
 
int fIndex = foodMenuList.getSelectedIndex();
ordfVec.add(new FoodOrd(((FoodMen)foodVec.elementAt(fIndex)).getFmName(),
  	((FoodMen)foodVec.elementAt(fIndex)).getFmPrice()));
  	
  	foodOrderList.setListData(ordfVec);
	


really need this help.
Thanks very very much !


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: JList problem Posted: Aug 4, 2003 2:26 AM
Reply to this message Reply
Woh...can you explain a little more (a) what you're trying to do (b) what's actually happening.

Also, I'd suggest turning this unreadable sludge:
int fIndex = foodMenuList.getSelectedIndex();
ordfVec.add(new FoodOrd(((FoodMen)foodVec.elementAt(fIndex)).getFmName(), ((FoodMen)foodVec.elementAt(fIndex)).getFmPrice()));
foodOrderList.setListData(ordfVec);

...into something more readable:
// Get selected food item
int fIndex = foodMenuList.getSelectedIndex();
FoodMen currentFoodMen = (FoodMen)foodVec.elementAt(fIndex);
 
// Get food item data
int currentFoodPrice = currentFoodMen.getFmPrice();
String currentFoodName = currentFoodMen.getFmName();
 
// Create a new order
FoodOrd newOrder = new FoodOrd(currentFoodPrice, currentFoodName);
ordfVec.add(newOrder);

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: JList problem Posted: Aug 5, 2003 10:51 AM
Reply to this message Reply
If you are getting a duplication of items in your JList
which is using a Vector fro its elements, then somewhere in your ocde is not checking to see if a new element is already an element and adding another one.

The Vector class has a method to check to see if an object is already present.

boolean contains(Object elem)

Suggest using this with an if block and only adding to the JList Vector object is its not contained in the Vector.

Flat View: This topic has 2 replies on 1 page
Topic: OLE events Previous Topic   Next Topic Topic: final abstract class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use