Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-click event on JList element

I have a JList with a DefaultListModel.

How I can make an item in a JList react to double-click event?

like image 282
Lobo Avatar asked Dec 03 '10 10:12

Lobo


People also ask

How do you check if a JList is selected?

To check if there are any selected items, use the following: boolean res = ! list. isSelectionEmpty();

What is the purpose of JList in Java?

JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .

How do you remove an element from a JList in Java?

To actually remove the item, we use the syntax . remove(int); where the integer is the index of the item you wish to remove. That is how we add and remove things from a JList, and this concludes our tutorial on the JList.

Which interface is implemented in JList?

Simple, dynamic-content, JList applications can use the DefaultListModel class to maintain list elements. This class implements the ListModel interface and also provides a java.


1 Answers

String[] items = {"A", "B", "C", "D"}; JList list = new JList(items);  list.addMouseListener(new MouseAdapter() {     public void mouseClicked(MouseEvent evt) {         JList list = (JList)evt.getSource();         if (evt.getClickCount() == 2) {              // Double-click detected             int index = list.locationToIndex(evt.getPoint());         } else if (evt.getClickCount() == 3) {              // Triple-click detected             int index = list.locationToIndex(evt.getPoint());         }     } }); 
like image 107
Mohamed Saligh Avatar answered Sep 25 '22 08:09

Mohamed Saligh