JComboBox: setSelectedIndex(int anIndex) : JComboBox « javax.swing « Java by API






JComboBox: setSelectedIndex(int anIndex)

  
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.plaf.ComboBoxUI;

public class MainClass extends JFrame {
  JComboBox jcb;

  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel jp = new JPanel();

    Vector v = new Vector();
    v.add("A");
    v.add("B");
    v.add("C");

    jcb = new JComboBox(v);
    jcb.setEditable(true);

    jcb.getActionMap().put("selectNext", new DownAction());

    jp.setPreferredSize(new Dimension(200, 35));
    jp.add(jcb);
    getContentPane().add(jp);

    pack();
    setVisible(true);
  }

  public static void main(String[] args) {
    new MainClass("Binding Demo3");
  }
}

class DownAction extends AbstractAction {
  public void actionPerformed(ActionEvent e) {
    JComboBox jcb = (JComboBox) e.getSource();
    System.out.println("down action");
    ComboBoxUI ui = jcb.getUI();

    if (ui.isPopupVisible(jcb)) {
      int i = jcb.getSelectedIndex();
      if (i < jcb.getModel().getSize() - 1) {
        jcb.setSelectedIndex(i + 1);
        jcb.repaint();
      }
    } else {
      int nItems = jcb.getItemCount();

      ComboBoxEditor cbe = jcb.getEditor();

      String st; // Search text

      st = ((String) cbe.getItem()).toUpperCase();

      for (int i = 0; i < nItems; i++) {
        String item = ((String) jcb.getItemAt(i)).toUpperCase();

        if (item.startsWith(st)) {
          jcb.setSelectedIndex(i);
          break;
        }
      }

      ui.setPopupVisible(jcb, true);
    }
  }
}

           
         
    
  








Related examples in the same category

1.implements JComboBox.KeySelectionManager
2.new JComboBox()
3.new JComboBox(ComboBoxModel aModel)
4.new JComboBox(Vector items)
5.JComboBox: addActionListener(ActionListener lis)
6.JComboBox: addItem(Object o)
7.JComboBox: addItemListener(ItemListener lis)
8.JComboBox: addPopupMenuListener(PopupMenuListener l)
9.JComboBox: getActionMap()
10.JComboBox: getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
11.JComboBox: getItemAt(int index)
12.JComboBox: getItemCount()
13.JComboBox: getKeySelectionManager()
14.JComboBox: getSelectedIndex()
15.JComboBox: getSelectedItem()
16.JComboBox: insertItemAt(Object anObject, int index)
17.JComboBox: isPopupVisible()
18.JComboBox: removeAllItems()
19.JComboBox: removeItemAt(int anIndex)
20.JComboBox: setEditor(ComboBoxEditor anEditor)
21.JComboBox: setKeySelectionManager(KeySelectionManager aManager)
22.JComboBox: setRenderer(ListCellRenderer aRenderer)
23.JComboBox: setEditable(boolean b)
24.JComboBox: setMaximumRowCount(int count)
25.JComboBox: setSelectedItem(Object anObject)
26.JComboBox: setUI(ComboBoxUI ui)