
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we catch a double click and enter key events for a JList in Java?\\n
In this article, we will learn to catch a double click and enter key events for a JList in Java. We will be using Java Swing to detect double click and Enter key events on a JList. When we double click an item in the list or press "Enter" key after selecting an item, the selected item is displayed.
What is a JList?
A JList can extend the JComponent class, which allows us to choose either a single or multiple selections. A JList can generate a ListSelectionListener interface, and it includes one abstract method, valueChanged().
Below is the graphical representation of a JList:
How to catch a double click and enter key events?
A JList can also generate a MouseListener interface to catch a double click event in the list and generate a KeyListener interface to catch an Enter key event.
mouseClicked() Method
The mouseClicked() is a method of the MouseListener interface and is called when a mouse button is clicked, meaning pressed, and released on a component. It takes only a single argument, as MouseEvent.
Syntax
The following is the syntax for the mouseClicked() method declaration:
void mouseClicked(MouseEvent e)
MouseListener
The MouseListener interface handles all the mouse interactions(press, release, click, enter, and exit). The class that processes the MouseEvent should implement this interface. The object for a MouseListener is registered using the addMouseListener() method.
keyReleased() Method
The keyReleased() is a method os the KeyListener interface and is called when a pressed key is released from a component. It takes only a single argument, as KeyEvent.
Syntax
The following is the syntax for the mouseClicked() method declaration:
void keyReleased(KeyEvent e)
KeyListener
The KeyListener interface handles all the key interactions(press and release). The class that processes the KeyEvent should implement this interface. The object for a KeyListener is registered using the addKeyListener() method.
Catching a Double Click and Enter Key Events for a JList
To catch a double click and Enter key events for a JList in Java, at first, we have to declare a class named "JListTest" that extends the JFrame, then we will initialize the constructor for the class, set the title of the frame as "JList Test", and use Flowlayout for default layout of the frame.
Creating a Vector with 14 items from "Item #1" to "Item #14" using the for loop:
Vector v = new Vector(); for (int i = 1; i < 15; i++) { v.addElement("Item #" + i); }
Adds two labeled JTextField pairs, one to display double-clicked items and the other to display items selected with the Enter key. Creates a JList component and adds the Vector data into the list.
final JList list = new JList(v);
Adds a mouse listener to detect double-clicks using the mouseClicked() method. So whenever you double click the mouse, the getClickCount() method counts the number of clicks, and if the click count is 2, it displays the selected item in the first textfield of the JTextField.
list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { if (me.getClickCount() == 2) { dblTextField.setText(""+list.getSelectedValue()); } } });
Adds a key listener with the keyReleased() method to detect when the "Enter" key is pressed. So whenever you press the "Enter" key on the keyboard, the getKeyCode() method stores the key pressed and checks if the key pressed is an "Enter" key, and if it matches, then it displays the selected item in the second textfield of the JTextField.
list.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_ENTER) { entTextField.setText(""+list.getSelectedValue()); } } });
Example
Below is an example of catching a double click and Enter key events for a JList in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; public class JListTest extends JFrame { public JListTest() { setTitle("JList Test"); setLayout(new FlowLayout()); Vector v = new Vector(); for (int i = 1; i < 15; i++) { v.addElement("Item #" + i); } add(new JLabel("Double-clicked on: ")); final JTextField dblTextField = new JTextField(15); add(dblTextField); add(new JLabel("Enter key on: ")); final JTextField entTextField = new JTextField(15); add(entTextField); final JList list = new JList(v); // catch double-click events list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { if (me.getClickCount() == 2) { dblTextField.setText(""+list.getSelectedValue()); } } }); // catch enter-key events list.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_ENTER) { entTextField.setText(""+list.getSelectedValue()); } } }); add(new JScrollPane(list)); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JListTest(); } }