Dropdown Issue


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Dropdown Issue

Hybrid View

  1. #1
    Join Date
    Mar 2006
    Posts
    13

    Dropdown Issue

    Hi Frnds,
    I have one question, i.e., i have a form with 2 Dropdown boxes.
    In that one should be static dropdown and another should be dynamic dropdown.

    while loading the page i am able to get the data for both the dropdowns.

    But my question is when we select any thing on first dropdown then the second one will display all the related list based on the first dropdown value.

    For example:
    The data in the DB is like this
    1-A, 1-B,1-C,1-D;
    2-M,2-N,2-O;
    3-X,3-Y,3-Z and so on.
    The first dropdown should display 1,2,3,4 and second one should display the corresponding values on it.

    Suppose if u select 1, then the second dropdown display A,B,C,D.
    If u select 2,then second dropdown display M,N,O and so on.

    Plz help me.. This is very urgent need for me.

    Thanks in advance..



    Thanks,
    - Sai

  2. #2
    Join Date
    Mar 2006
    Location
    England
    Posts
    10

    Post

    Here's some code that includes what you're looking for. The significant part is the SecondaryListModel.updateContents(). Of course, you will need to pick and update the code below depending on how you setup the list contents. You can run em for test purposes. Good luck!

    Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class Program
    {
        public static void main(String[] args)
        {
            new Program();
        }
        
        private Map multiLevelMap;
        private JComboBox dropDownA, dropDownB;
        private SecondaryListModel dropDownBModel;
        
        public Program()
        {
            JFrame frame = new JFrame("Drop-down lists");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JPanel panel = new JPanel();
            
            multiLevelMap = new HashMap();
            String[] dropDownAItems = new String[] { "1", "2", "3" };
            multiLevelMap.put(dropDownAItems[0], new String[] {"A","B","C","D"});
            multiLevelMap.put(dropDownAItems[1], new String[] {"M","N","O"});
            multiLevelMap.put(dropDownAItems[2], new String[] {"X","Y","Z"});
            dropDownA = new JComboBox(dropDownAItems);
            
            dropDownBModel = new SecondaryListModel((Object[])multiLevelMap.get("1"));
            dropDownB = new JComboBox(dropDownBModel);
            dropDownA.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Object[] newContents = (Object[]) multiLevelMap.get(dropDownA.getSelectedItem());
                    dropDownBModel.updateContents(newContents);
                }
            });
            
            panel.add(dropDownA);
            panel.add(dropDownB);
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setSize(300, 100);
            frame.setVisible(true);
        }
        
        class SecondaryListModel extends DefaultComboBoxModel
        {
            SecondaryListModel(Object[] initialContents)
            {
                super(initialContents);
            }
            void updateContents(Object[] newContents)
            {
                removeAllElements();
                for (int n=0; n<newContents.length; ++n) {
                    addElement(newContents[n]);
                }
                fireContentsChanged(this, 0, newContents.length-1);
            }
        }
    }

  3. #3
    Join Date
    Mar 2006
    Posts
    13

    Thanks

    Hi,
    Thanks for your help.
    I will try this...

    Thanks,
    - Saikiran

Similar Threads

  1. dropdown list
    By abenitez77 in forum VB Classic
    Replies: 2
    Last Post: 05-09-2014, 12:00 AM
  2. Dropdowns issue
    By saikiran_kris in forum Java
    Replies: 0
    Last Post: 03-14-2006, 01:47 PM
  3. Dropdown Box troubles
    By joe_coolish in forum ASP.NET
    Replies: 1
    Last Post: 05-26-2005, 03:07 PM
  4. auto-fill in dropdown box
    By Kevin M. in forum ASP.NET
    Replies: 1
    Last Post: 07-18-2002, 09:32 AM
  5. Replies: 0
    Last Post: 09-19-2001, 07:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center