Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Reading from ListBox.  RSS feed

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm wondering about how exactly you read an item from a list box.
I tried using the getSelectedIndex() and comparing to a string, but that didn't seem to work.
Could it be that I have to load the listbox items into an array, and from there compare with getSelectedIndex()?
any ideas would be helpful.
thanks
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're talking about a JList, I think this is what you're looking for:
<code>
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
dataList.setSelectedIndex(1); // selects "two"

//Gets the item from the list
//You must cast it to a String
String item = (String)dataList.getSelectedValue();
if (item.equals("two"))
{
System.out.println(item);//prints two
}
<code>
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou, I'll give that a try.
But..
I just ran across getSelectedValue().
When would I use getselectedIndex() over getSelectedValue()?
Plus, what are the diff's good and bad about each.
[ November 10, 2002: Message edited by: Drake Silver ]
 
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you even bothered to look at the API for a JList or List yet?
You'll notice that getSelectedIndex() returns an INT and getSelectedValue() returns an OBJECT. That is the difference between the 2. There is not really a + or - to using either one. It depends on how you want to retrieve the data. However, if you are actually trying to retrieve the "String" of an item in your JList or List, use getSelectedValue() and cast the object to a String.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!