- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Java Program to set Selection Mode for JList only for single selection
To set single selection for JList, use DefaultListSelectionModel and set it to SINGLE_SELECTION:
String values[]= { "One","Two","Three","Four","Five","Six"}; JList list = new JList(values); list.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
The following is an example to set the selection mode for JList only for single selection:
Example
package my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { static JFrame frame; static JList list; public static void main(String[] args) { frame = new JFrame("JList Demo"); SwingDemo s = new SwingDemo(); JPanel panel = new JPanel(); String values[]= { "One","Two","Three","Four","Five","Six"}; list = new JList(values); list.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); panel.add(list); frame.add(panel); frame.setSize(550,300); frame.setVisible(true); } }
The output is as follows. Now, you can only select a single item
Output
- Related Articles
- How many types of selection modes for a JList in Java?
- Python Program for Selection Sort
- 8086 program for selection sort
- C Program for Selection Sort?
- C Program for Activity Selection Problem
- Python Program for Activity Selection Problem
- How to set Mnemonic key for selection of each JCheckBox in Java?
- C++ program for Sorting Dates using Selection Sort
- Java program to implement selection sort
- Can we set JOptionPane with predefined selection in Java?
- Java Program to enable cell selection in a JTable
- Java Program to enable column selection in a JTable
- Selection sort in Java.
- C++ Program to Implement Selection Sort
- How to get or set the selection state of JCheckBox in Java

Advertisements