Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Samual Sam

To select the second index, use the setSelectedIndex() method −

JList new JList(sports);
list.setSelectedIndex(2);

The following is an example to select the second index in Java 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[]= { "Cricket","Football","Hockey","Rugby"};
      list = new JList(sports);
      list.setSelectedIndex(2);
      panel.add(list);
      frame.add(panel);
      frame.setSize(400,400);
      frame.setVisible(true);
   }
}

Output

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.