Java Swing How to - Pre select item by index








Question

We would like to know how to pre select item by index.

Answer

import javax.swing.JComboBox;
import javax.swing.JFrame;
/*from  www.  j  a  va  2s. c  o  m*/
public class Main {

  public static void main(String[] args) {
    JComboBox<String> my = new JComboBox<>(new String[] {
        "HELLO WORLD", "java2s.com" });
    JFrame frame = new JFrame("");
    frame.add(my);
    frame.pack();
    frame.setVisible(true);
    my.setSelectedIndex(1);
  }
}