- 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
Java Program to select all the items in a JList
To select all the items in a JList, use setSelectionInterval() and set a range:
JList list = new JList(sports); int begn = 0; int end = list.getModel().getSize() - 1; if (end >= 0) { list.setSelectionInterval(begn, end); }
The following is an example to select all the items in a JList:
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 sports[]= {"Football","Fencing","Cricket","Squash","Hockey","Rugby"}; list = new JList(sports); int begn = 0; int end = list.getModel().getSize() - 1; if (end >= 0) { list.setSelectionInterval(begn, end); } panel.add(list); frame.add(panel); frame.setSize(550,300); frame.setVisible(true); } }
Output
- Related Articles
- Java Program to select the first item in JList
- How to select the second index in Java JList?
- How to display a value when select a JList item in Java?
- Java Program to select all cells in a table
- How can we add different font style items to JList in Java?\n
- How to display the JList items from top to bottom and left to right in Java?
- Python Program to Multiply All the Items in a Dictionary
- Java Program to check if the second item is selected in Java JList
- How can I check if there are any selected items in Java JList
- How can we clear all selections in Java Swing JList?
- Python program to find the sum of all items in a dictionary
- Java Program to select a column in JTable?
- How to add ScrollBar to JList in Java?
- Java Program to set Selection Mode for JList only for single selection
- How to store string array in Java JList?

Advertisements