Swinging Duke
Feedback Button
Left ArrowRight Arrow

 

Swing Combo Box Controls

Swing creates and manages combo boxes using the JComboBox class, which implements a classic combo box. You can also create customized combo boxes using the JComboBox class.

JComboBox shares the following features with other kinds of list boxes:

    cb.setEditable(true);

The following code fragment shows how a classic combo box can be implemented in a program that makes use of Swing. The example comes from a source file named ComboBoxPanel.java, which is part of the SwingSet sample program that is distributed with Swing. The code creates a classic combo box, an editable combo box, and a customized combo box with an item list that can be manipulated using tree-node operations:

    public ComboBoxPanel(SwingSet swing) {
      JPanel tp;
      this.swing = swing;
      this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
      setBorder(swing.emptyBorder5);
      
      this.add(Box.createRigidArea(new Dimension(1,50)));
      JPanel panel = new JPanel(false);
      panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
      add(panel);
      // Classic combo box
      tp = new JPanel();
      tp.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
      tp.setBorder(BorderFactory.createTitledBorder
        ("Classic ComboBox"));
		// tp.setTitle("Classic ComboBox");
      tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
      tp.add(Box.createRigidArea(new Dimension(5,1)));
      months = new JComboBox();
      months.addItem("January");
      months.addItem("February");
      months.addItem("March");
      months.addItem("April");
      months.addItem("May");
      months.addItem("June");
      months.addItem("July");
      months.addItem("August");
      months.addItem("September");
      months.addItem("October");
      months.addItem("November");
      months.addItem("December");
      tp.add(months);
      tp.add(Box.createRigidArea(new Dimension(5,1)));
      days = new JComboBox();
      days.addItem("Monday");
      days.addItem("Tuesday");
      days.addItem("Wednesday");
      days.addItem("Thursday");
      days.addItem("Friday");
      days.addItem("Saturday");
      days.addItem("Sunday");
      tp.add(days);
      tp.add(Box.createRigidArea(new Dimension(5,1)));
      panel.add(tp);

      // Editable combo box

      add(panel);
      tp = new JPanel();
      tp.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
      tp.setBorder(BorderFactory.createTitledBorder("Editable ComboBox"));
      tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
      tp.add(Box.createRigidArea(new Dimension(5,1)));
      cb = new JComboBox();
      cb.setEditable(true);
      cb.addItem("0");
      cb.addItem("10");
      cb.addItem("20");
      cb.addItem("30");
      cb.addItem("40");
      cb.addItem("50");
      cb.addItem("60");
      cb.addItem("70");
      cb.addItem("80");
      cb.addItem("90");
      cb.addItem("100");
      cb.addItem("More");
      cb.setSelectedItem("50");
      tp.add(cb);
      tp.add(Box.createRigidArea(new Dimension(5,1)));

      cb1 = new JComboBox();
      cb1.setEditable(true);
      cb1.addItem("0");
      cb1.addItem(".25");
      cb1.addItem(".5");
      cb1.addItem(".75");
      cb1.addItem("1.0");
      cb1.setSelectedItem(".5");
      tp.add(cb1);

      tp.add(Box.createRigidArea(new Dimension(5,1)));
      panel.add(tp);

      // Custom combobox
      panel = new JPanel(false);
      panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
      add(panel);
      tp = new JPanel();
      tp.setMaximumSize(new Dimension(Short.MAX_VALUE,200));
      tp.setBorder(BorderFactory.createTitledBorder("Custom ComboBox"));
		// tp.setTitle("Custom ComboBox");
      tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
      tp.add(Box.createRigidArea(new Dimension(5,1)));
      custom = new CustomComboBox(new CustomComboBoxModel());
      custom.setRenderer(new TestCellRenderer(custom));
      ((CustomComboBox)custom).setCanChangeRenderer(false);
      custom.setSelectedItemIndex(0);
      custom.setMaximumRowCount(4);
      tp.add(custom);
      tp.add(Box.createRigidArea(new Dimension(5,1)));

      DefaultMutableTreeNode swingNode = new DefaultMutableTreeNode("Swing");
      DefaultMutableTreeNode spec  = new DefaultMutableTreeNode("spec");
      DefaultMutableTreeNode api   = new DefaultMutableTreeNode("api");

      swingNode.add(spec);
      swingNode.add(api);

      api.add(new DefaultMutableTreeNode("JComponent"));
      api.add(new DefaultMutableTreeNode("JTable"));
      api.add(new DefaultMutableTreeNode("JTree"));
      api.add(new DefaultMutableTreeNode("JComboBox"));
      api.add(new DefaultMutableTreeNode("JTextComponent"));

      spec.add(new DefaultMutableTreeNode("JComponent"));
      spec.add(new DefaultMutableTreeNode("JTable"));
      spec.add(new DefaultMutableTreeNode("JTree"));
      spec.add(new DefaultMutableTreeNode("JComboBox"));
      spec.add(new DefaultMutableTreeNode("JTextComponent"));
      
      treeComboBox = new TreeCombo(new DefaultTreeModel(swingNode));
      tp.add(treeComboBox);
      tp.add(Box.createRigidArea(new Dimension(5,1)));
      panel.add(tp);
    }

More comprehensive documentation for the JComboBox class in now being developed. Meanwhile, you can learn more about how the BorderFactory works by consulting the JComboBox API and by examing the code in the SwingSet sample application.

Arrows


Version 0.5. Last modified 09/18/97.
Copyright © 1995-97 Sun Microsystems, Inc. All Rights Reserved.

Sun's Home Page