Search code examples
javaswingjcomboboxjmenumulti-level

How to create a multi-layer JComboBox


I'm creating a simulator using Java Swing. I used JComboBox to display units of utilities such as "KW, KL, KM" etc to measure Power, Water and distance. It's simple to add bunch of items to a JComboBox. User select a unit and the JFrame will save the selection when a "save" button is clicked.

    JComboBox comboBox = new JComboBox();
    for(ValueUnits u: ValueUnits.values()){
        comboBox.addItem(u.returnUnits());
    }

    comboBox.setSelectedIndex(-1);
    unitColumn.setCellEditor(new DefaultCellEditor(comboBox));

Now I want to create an multi-layer JComboBox (perhaps JMenu?). The function of such should behave as a multi-layer JMenu. When the JComboBox is clicked, it will show the first layer - category such as "Electricity, Water, Distance...", Then when mouse hover over Electricity, a list of Electricity units such as "KW, MW, W ..." will show. These collections are fetched from Enumerations. I wonder what's the most correct way to create such component.

Thank you so much world!


Solution

  • Maybe use 2 combo boxes? That is you select a value in the first and the units are displayed in the second:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class ComboBoxTwo extends JPanel implements ActionListener
    {
        private JComboBox<String> mainComboBox;
        private JComboBox<String> subComboBox;
        private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();
    
        public ComboBoxTwo()
        {
            String[] items = { "Select Item", "Color", "Shape", "Fruit" };
            mainComboBox = new JComboBox<String>( items );
            mainComboBox.addActionListener( this );
    
            //  prevent action events from being fired when the up/down arrow keys are used
            mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
            add( mainComboBox );
    
            //  Create sub combo box with multiple models
    
            subComboBox = new JComboBox<String>();
            subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
            add( subComboBox );
    
            String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };
            subItems.put(items[1], subItems1);
    
            String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };
            subItems.put(items[2], subItems2);
    
            String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };
            subItems.put(items[3], subItems3);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            String item = (String)mainComboBox.getSelectedItem();
            Object o = subItems.get( item );
    
            if (o == null)
            {
                subComboBox.setModel( new DefaultComboBoxModel() );
            }
            else
            {
                subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
            }
        }
    
        private static void createAndShowUI()
        {
            JFrame frame = new JFrame("SSCCE");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add( new ComboBoxTwo() );
            frame.setLocationByPlatform( true );
            frame.pack();
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }