Get Selected Index From Single Select AWT List Example

  1. /*
  2. Get Selected Index From Single Select AWT List Example
  3. This java example shows how to get selected index of single select AWT
  4. List control.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Graphics;
  9. import java.awt.List;
  10. import java.awt.event.ItemEvent;
  11. import java.awt.event.ItemListener;
  12.  
  13. /*
  14. <applet code="GetSelectedIndexFromSingleSelectExample" width=200 height=200>
  15. </applet>
  16. */
  17.  
  18. public class GetSelectedIndexFromSingleSelectExample extends Applet implements ItemListener{
  19.  
  20. List list = null;
  21.  
  22. public void init(){
  23.  
  24. //create a single select list
  25. list = new List(5, false);
  26.  
  27. //add items to a list
  28. list.add("One");
  29. list.add("Two");
  30. list.add("Three");
  31. list.add("Four");
  32. list.add("Five");
  33. list.add("Six");
  34. list.add("Seven");
  35.  
  36. //add list
  37. add(list);
  38.  
  39. //add listener
  40. list.addItemListener(this);
  41.  
  42. }
  43.  
  44. public void paint(Graphics g){
  45. /*
  46. * To get selected index from a single select list, use
  47. * String getSelectedIndex()
  48. * method of AWT List class.
  49. *
  50. * NOTE : getSelectedIndex method returns -1 if no items were selected or
  51. * multiple items were selected.
  52. */
  53.  
  54. g.drawString("Selected Index: "+ list.getSelectedIndex(), 10, 120);
  55. }
  56.  
  57. public void itemStateChanged(ItemEvent arg0) {
  58. repaint();
  59. }
  60.  
  61. }

Example Output

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!