Java JComboBox get editor component

Description

Java JComboBox get editor component

import java.awt.FlowLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;

class Demo extends JPanel {
  public Demo() {    
    setLayout(new FlowLayout());
    /* w  ww.ja  v  a  2s .com*/
    JComboBox<String> jcb = new JComboBox<String>();
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
 
    model.addElement("CSS");
    model.addElement("HTML");
    model.addElement("Java");
    
    jcb.setModel(model);
    
    jcb.setEditable(true);
    
    JTextComponent textField = (JTextComponent) jcb.getEditor().getEditorComponent();
    
    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