Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
127 views
in JAVA by (178k points)
How to set the selection mode of a JList in Java?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

The selection mode of a JList determines how many items the user can select in the list. By default, the selection mode of a JList is set to SINGLE_SELECTION, which allows the user to select only one item at a time. However, you can set the selection mode to one of the following values:

  • SINGLE_SELECTION: allows the user to select only one item at a time (default)
  • SINGLE_INTERVAL_SELECTION: allows the user to select a contiguous range of items
  • MULTIPLE_INTERVAL_SELECTION: allows the user to select any number of non-contiguous items

To set the selection mode of a JList, you can use the setSelectionMode method, which takes an integer constant as an argument. 

Here's an example:

JList<String> list = new JList<String>(items);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
 

In this example, the JList is created with an array of items as before. Then, the setSelectionMode method is called on the JList instance to set the selection mode to MULTIPLE_INTERVAL_SELECTION, which allows the user to select any number of non-contiguous items.

You will also need to import the ListSelectionModel class from the javax.swing package in order to use the selection mode constants:

import javax.swing.ListSelectionModel;
 

You can also get the current selection mode of a JList by calling the getSelectionMode method:

int selectionMode = list.getSelectionMode();
 

This will return an integer constant representing the current selection mode of the JList.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked May 8, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 8, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 8, 2023 in JAVA by kvdevika (178k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...