INTRODUCTION OF JCOMBOBOX

Introduction of JCombobox


JComboBox

    Java Swing Tutorial Explaining the JComboBox Component. JComboBox is like a drop down box — you can click a drop-down arrow and select an option from a list. It generates ItemEvent. For example, when the component has focus, pressing a key that corresponds to the first character in some entry’s name selects that entry. A vertical scrollbar is used for longer lists.

Example:JCombobox

 
            
                package com.swing.sdj;
                import java.awt.BorderLayout;
                import java.awt.Dimension;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import javax.swing.BorderFactory;
                import javax.swing.ImageIcon;
                import javax.swing.JComboBox;
                import javax.swing.JFrame;
                import javax.swing.JLabel;
                import javax.swing.JPanel;
                public class JComboBoxDemo  extends JPanel
                {
                    JLabel jlbPicture;
                    public  JComboBoxDemo()
                    {
                        String[] comboTypes = { "J2SE", "J2EE", "STRUTS","HIBERNATE","SPRING" };
                        JComboBox comboTypesList = new JComboBox(comboTypes);
                        comboTypesList.setSelectedIndex(2);
                        comboTypesList.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                JComboBox jcmbType = (JComboBox) e.getSource();
                                String cmbType = (String) jcmbType.getSelectedItem();
                                jlbPicture.setIcon(new ImageIcon("D:\sdj.jpg"+ cmbType.trim().toLowerCase() + ".jpg"));
                            }
                        });
                        /* Set up the picture */
                        jlbPicture = new JLabel(new ImageIcon("D:\sdj.jpg"+comboTypes[comboTypesList.getSelectedIndex()] + ".jpg"));
                        jlbPicture.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
                        jlbPicture.setPreferredSize(new Dimension(177, 122 + 10));
                        // Layout the demo
                        setLayout(new BorderLayout());
                        add(comboTypesList, BorderLayout.NORTH);
                        add(jlbPicture, BorderLayout.SOUTH);
                        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
                    }
                    public static void main(String s[])
                    {
                        JFrame frame = new JFrame("SDJ JComboBox Usage Demo");
                        frame.setContentPane(new JComboBoxDemo());
                        frame.pack();
                        frame.setVisible(true);
                    }
                } 
            

Output:

Naming Convention
SDJ JComboBox Demo
Naming Convention
SDJ JComboBox Demo