Java DefaultComboBoxModel add element

Description

Java DefaultComboBoxModel add element

import java.awt.FlowLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Demo extends JPanel {
  public Demo() {    
    setLayout(new FlowLayout());
    // w  ww .j  av  a  2s . c  o m
    JComboBox<String> jcb = new JComboBox<String>();
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
 
    model.addElement("CSS");
    model.addElement("HTML");
    model.addElement("Java");
    
    jcb.setModel(model);
    add(jcb);
    jcb.setSelectedIndex(1);
  }
}

public class Main {
  public static void main(String[] args) {
    Demo panel = new Demo();
    JFrame application = new JFrame();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add(panel);
    application.setSize(250, 250);
    application.setVisible(true);
  }
}



PreviousNext

Related