Java List double click event implementation method

  • 2020-04-01 03:29:45
  • OfStack

This paper briefly introduces the implementation method of double click event in Java List, which has good reference value. Share with you for your reference. Specific methods are as follows:

1. Define a MouseListener;

2. Increase the mouseClicked event in mouseListener;

3. Get the List object by getSource() of MouseEvent;

4. Get the Index of the clicked item by the getSelectedIndex() event of the List;

5. According to Index, getItem() method of List is used to obtain the clicked item;

Finally, add the MouseListener() definition to the List.


//Double click event
MouseListener mouseListener = new MouseAdapter() {
   public void mouseClicked(MouseEvent mouseEvent) {
 List theList = (List) mouseEvent.getSource();
 if (mouseEvent.getClickCount() == 2) {
   int index = theList.getSelectedIndex();
   if (index >= 0) {
  String s = theList.getItem(index);
   }
 }
   }
};
lstRoster.addMouseListener(mouseListener);

I hope this article has been helpful to your Java programming.


Related articles: