I have a JList
with a DefaultListModel
.
How I can make an item in a JList
react to double-click event?
To check if there are any selected items, use the following: boolean res = ! list. isSelectionEmpty();
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 .
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.
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.
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()); } } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With