Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: AWT
Expertise: Beginner
Aug 10, 1999

List Selection Actions

Question:
When someone selects one item on my list, I want an input field to appear next to it. How do I do this?

Answer:
Rather than provide you with the code to do this, I will describe the concepts you need in order to implement this. When you select an item in a List, an ItemEvent is generated and forwarded to all of the List's registered ItemListeners. You can register an ItemListener with the addItemListener method and remove one with removeItemListener. So the first step you have to take is to create an ItemListener that will react to the selection of a list item. An ItemEvent indicating the selection of a list item will return ItemEvent.SELECTED when its getStateChange() method is called. If an item was deselected, getStateChange() will return ItemEvent.DESELECTED.

Once it has identified a list selection, your ListItemListener has to take care of adding a TextField to your GUI.

You will run into problems if you are using java.awt.List and want to place the field right next to the selected item. The List class doesn't provide a mechanism for determing the bounding coordinates of an item. If you need this information, you should use javax.swing.JList instead. It provides a method called Rectangle getBounds(int index1, int index2) which will give you the bounding rectangle for a range of items. You can use this information to determine the x and y coordinates at which to place your TextField. In addition, if you use JList, you can place the TextField in the same JScrollPane as the JList, so the field will scroll along with the list.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap