How would I code when a item has been selected in a JList?

为君一笑 提交于 2020-02-03 14:06:39

问题


I have a JList with 5 options in it and when one of the items becomes selected or clicked i want the text area next to it to show a paragraph of text relative to the item clicked. It should do this for each item in the list but I cant seem to find how to do it in the API

How would my program know if an item in the JList was selected so I can work with the data?


回答1:


Use addListSelectionListener. You can create a subclass (anonymous or not) of ListSelectionListener that does the work you want.

myList.addListSelectionListener(new ListSelectionListener()
{
  public void valueChanged(ListSelectionEvent ev)
  {
    // handle ev
  } 
});



回答2:


You should register a Listener for events on your JList. When the Swing UI fires one off, this Listener class will get the message and react accordingly.



来源:https://stackoverflow.com/questions/3114721/how-would-i-code-when-a-item-has-been-selected-in-a-jlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!