- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
A JList can extend JComponent class that allows the user to choose either a single or multiple selections. A JList can generate a ListSelectiionListener interface and it includes one abstract method valueChanged(). A JList can also generate a MouseListener interface to catch a double click event in the list and generates a KeyListener interface to catch an enter key event.
Example
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(); } }
Output
- Related Articles
- How can we detect the double click events of a JTable row in Java?
- How can we add different font style items to JList in Java?\n
- Can we have a try block without a catch block in Java?\n
- How can we use JavaScript Executor to click and enter data to a web element in Selenium?
- How can we clear all selections in Java Swing JList?
- Can we to override a catch block in java?
- Can we declare a try catch block within another try catch block in Java?
- Can we cast a double value to a byte in java?
- How can we sort a JSONObject in Java?\n
- How can we set the shortcut key to a JButton in Java?
- Can we define a try block with multiple catch blocks in Java?
- How can we implement right click menu using JPopupMenu in Java?
- How can we create a login form in Java?\n
- How can we initialize a boolean array in Java?\n
- How can we sort a Map by both key and value using lambda in Java?

Advertisements